Wednesday, February 25, 2009

How to Connect to Team Foundation Server Using C#

In this article I will show you how to connect to Team Foundation Server and add work item via code
First thing you must install TFS SDK, you can download the Team Foundation Server SDK from the below link.
http://www.microsoft.com/downloads/details.aspx?FamilyID=7e0fdd66-698a-4e6a-b373-bd0642847ab7&DisplayLang=en
Steps:
1- Create windows or web Application
2- Add References for :
Microsoft.TeamFoundation.Client
Microsoft.TeamFoundation.WorkItemTracking.Client
3- The below code will help you to work with Team Foundation Server

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

// declaration variable
private NetworkCredential networkCredential = new NetworkCredential("enter here User Name", "enter here password", "enter here domian");
private Uri uri = new Uri("enter here TFS URL");
string projectName="enter here TFS prject name";
string worktemType = "enter here Work Item Type";

// Set Authentication To TFS
TeamFoundationServer teamFoundationServer = new TeamFoundationServer(uri.AbsoluteUri, networkCredential);
teamFoundationServer.Authenticate();
//Add Work Item
WorkItemStore wis = (WorkItemStore)teamFoundationServer.GetService(typeof(WorkItemStore));
Project tfsProject = wis.Projects[projectName];
WorkItemType wiType = tfsProject.WorkItemTypes[workItemType];
WorkItem workItem = new WorkItem(wiType);
workItem.Title = "Test Work Item";
workItem.Description = "Work Item Description";
workItem.Save();

7 comments:

  1. how to get all the history of the project?
    macs8000@yahoo.co.uk.
    do u have a code?

    ReplyDelete
  2. I get the error message when I try to 'Authenticate'

    System.Net.WebException = {"The proxy name could not be resolved: 'novalue'"}

    Any ideas?

    Thanks

    ReplyDelete
  3. For Proxy Error Please check:
    http://forums.asp.net/t/992641.aspx

    ReplyDelete
  4. To Get History Please Check The Below URL:
    http://blogs.microsoft.co.il/blogs/srlteam/archive/2009/06/14/how-to-get-a-file-history-in-tfs-source-control-using-code.aspx

    ReplyDelete
  5. Can you please let me know that i wana get the Root Folders of my team projects...How can i do that?

    ReplyDelete
  6. The Structure is as follows:
    TeamProject
    1st project
    2nd project
    3rd project

    and so on i have 10 more projects all on same level

    I want to know the names of all these projects or their paths in VB.NET...Kindly help ?

    ReplyDelete