Jul 30, 2007

Connecting to a VS Team Foundation Server and fetching Work Item Details

  1. Download the VS TFS SDK (http://msdn2.microsoft.com/en-us/library/bb130146%28VS.80%29.aspx) or install the Visual Studio Team Suite. This will install the necessary assemblies required to access the VS TFS.
  2. Create a new VS 2005 Project.
  3. Browse to the following path (\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies) and add the following references

    -using Microsoft.TeamFoundation;
    -using Microsoft.TeamFoundation.Common;
    -using Microsoft.TeamFoundation.Client;
    -using Microsoft.TeamFoundation.WorkItemTracking.Client;
  4. Use the following code to access the VS TFS:

    TeamFoundationServer tfs = null;
    WorkItemStore wis = null;

    tfs = new TeamFoundationServer(tfsName, CredentialCache.DefaultCredentials);
    tfs.Authenticate();
    // Display the details about the TeamFoundationServer.
    richTextBox1.Text = "Team Foundation Server details:";
    richTextBox1.Text += " \n Server Name: " + tfs.Name;
    richTextBox1.Text += " \n Uri: " + tfs.Uri;
    richTextBox1.Text += " \n AuthenticatedUserDisplayName: " + tfs.AuthenticatedUserDisplayName;
    richTextBox1.Text += " \n AuthenticatedUserName: " + tfs.AuthenticatedUserName;
  5. To Access the Projects inside the TeamFoundationServer:

    wis = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
    //Iterate Through Projects
    foreach (Project tfs_project in wis.Projects)
    {
    treeView2.Nodes.Add(tfs_project.Name);
    }
  6. To Access the Workitems in the Projects:

    private void treeView2_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
    {
    string tfs_projectName = e.Node.Text.Trim(); //The project name

    //Perform WIQL Query
    WorkItemCollection wic = wis.Query(
    " SELECT [System.Id], [System.WorkItemType]," +
    " [System.State], [System.AssignedTo], [System.Title] " +
    " FROM WorkItems " +
    " WHERE [System.TeamProject] = '" + tfs_projectName +
    "' ORDER BY [System.WorkItemType], [System.Id]");


    foreach (WorkItem wi in wic)
    {
    TreeNode newnode = new TreeNode();
    newnode.Text = wi.Id + wi.Title + "-" + wi.Description;
    newnode.Tag = wi;
    treeView1.Nodes.Add(newnode);
    //treeView1.Nodes.Add(wi.Title + "[" + wi.Type.Name + "]" + wi.Description );
    }

Issues: While connecting from home I found that it takes really long, or sometimes times out during performing the following:
wis = (WorkItemStore)tfs.GetService(typeof(WorkItemStore))

The solution that I found in the net was :
Go to Internet Exploreer> Connections> LAN Settings
Instead of having a "Detect Proxy Automatically", put in a definite "Proxy setting"

Jul 5, 2007

Show a context menu when right clicking on a treeview

private void tvwMenu_MouseUp(object sender, MouseEventArgs e)
{
//Select Node When Right Clicked & Set Context Menu To Use
if( e.Button == MouseButtons.Right )
{
TreeNode node = tvwMenu.GetNodeAt( new Point( e.X, e.Y ) );
if( node != null )
{
TreeNodeMenuTag tag = (TreeNodeMenuTag)node.Tag;
tvwMenu.SelectedNode = node;
tvwMenu.ContextMenuStrip = (tag.isMenu) ? cmnuMenuNode :
cmnuProgramNode;
}
}
}

Enum Functions

  1. Iterating through an Enum:

    foreach (enmResponseInterval value in Enum.GetValues(typeof(enmResponseInterval)))
    {
    cmbXXX.Items.Add(value.ToString());
    }

  2. Conditional Switch Statement with Enumeration String Value
    public enum Product
    {
    Word,
    Outlook,
    Excel
    }

    public void Process(string productName)
    {
    switch ((Product)Enum.Parse(typeof(Product), productName.ToString()))
    {
    case Product.Excel:
    // Do something
    break;
    case Product.Outlook:
    // Do Something
    break;
    case Product.Word:
    // Do something
    break;
    }
    }