Saturday, March 1, 2014

Create Task in TFS Using C# Code

Hi All,
Now a day’s most people make use of Team Foundation Server. We perform any activity either in form or a Task or a Bug. I have come up with a sample code by using which we can create a Task in the TFS server using Programming or C# code. I have try this With Team Foundation Server 2010 and Visual Studio 2010. For doing this we required the 2 important DLL’s of team Foundation Server which we can find while adding reference in the .Net section, the DLL’s are


                  Microsoft.TeamFoundation.Client

           Microsoft.TeamFoundation.WorkItemTracking.Client



We will make use of a code and will try to add a task in our TFS repository, for this we required the details to connect to our TFS server like the Root folder name, Username, Port and Password details.

Either task or Bug both the Part of the Work Item so we will create the object of the Work Item Class like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace SampleTFSTask
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(" // Connect to the TFS, and get the WorkItemType object");
           
            Uri collectionUri = (args.Length < 1) ?
                new Uri("http://tfs.abc.com/MyGroup/Abc") : new Uri(args[0]);

            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(collectionUri);

            WorkItemStore workItemStore = tpc.GetService();

            Project teamProject = workItemStore.Projects["MVP"];

            WorkItemType workItemType = teamProject.WorkItemTypes["User Story"];

            // Create the work item.
            WorkItem userStory = new WorkItem(workItemType)
            {
                // The title is the only required field that does not have a default value.
                // You must set it, or you cannot save the work item.
                Title = "Recently awarded MVP",
                Description = " I want to see List of all MVP."
            };

            // Save the new user story.
             userStory.Save();
        }
    }
}


Hope You find this piece of information useful. Please feel free to extend and provide comment on the same.

Thanks
Anil Kumar Pandey
Microsoft MVP, Microsoft MCC, DNS MVM

Kontera