Retreiving Work Items from TFS

I'm currently playing around with the TFS API. It took me a while to get it working. In the process I found a couple valuable blog posts that helped me getting it to work.

Adding References

I found a fantastic blog post that suppose to add the team foundcation assemblies to the GAC. Therefore, one can add reference in VS from the .net tab.

This is nice, however, as I'm considering sharing the solution to public (once it's in a better state..hehe), I prefer adding the assemblies manually one by one and collect the ones I need in a folder called 'Dependencies'.

  • Here is the list of files in the dependencies folder so far:
  • Microsoft.TeamFoundation.WorkItemTracking.Proxy.dll
  • Microsoft.TeamFoundation.WorkItemTracking.Controls.dll
  • Microsoft.TeamFoundation.WorkItemTracking.Client.RuleEngine.dll
  • Microsoft.TeamFoundation.WorkItemTracking.Client.QueryLanguage.dll
  • Microsoft.TeamFoundation.WorkItemTracking.Client.Provision.dll
  • Microsoft.TeamFoundation.WorkItemTracking.Client.dll
  • Microsoft.TeamFoundation.WorkItemTracking.Client.DataStore.dll
  • Microsoft.TeamFoundation.WorkItemTracking.Client.Cache.dll
  • Microsoft.TeamFoundation.dll
  • Microsoft.TeamFoundation.Common.Library.dll
  • Microsoft.TeamFoundation.Common.dll
  • Microsoft.TeamFoundation.Client.dll

(they are all copied from Program files/Microsoft Visual Studio 9.0/Common7/IDE/Private assemblies)
and of course in there, I've got nunit.framework.dll and Rhino.Mocks.dll for TDD purposes(-:

Construction

There are a couple of ways to create connection with the TFS. This post explains the consideration choosing between using a factory or constructor to create connection with TFS. In this post, there are even some sample codes.

In my case, I chose to use the constructor as I might let the end user to switch between credentials on runtime. Something that using the factory cannot do.

Implementation

I'm using a repository pattern for my implementation. In this simplistic implementation, I chose to ignore the different type of work items (scenario, bugs, etc) and simply treating them as story cards.

    public class WorkItemRepository : IWorkItemRepository
    {
        #region IWorkItemRepository Members

        public IList GetWorkItems(string tfsUrl, string project, string DTSQuery,
                                              NetworkCredential credentials)
        {
            //https://blogs.msdn.com/buckh/comments/755115.aspx
            var tfs = new TeamFoundationServer(tfsUrl, credentials);

            tfs.EnsureAuthenticated();

            var store = new WorkItemStore(tfs);

            WorkItemCollection queryResult = store.Query(DTSQuery, new System.Collections.Hashtable \{\{"project", project\}\});

            IList result = new List();

            foreach(WorkItem item in queryResult)
            {
                IStoryCard storyCard = new StoryCard()
                                           {
                                               Story = item.Description
                                           };
                result.Add(storyCard);
            }
 
            return result;
        }

        #endregion
    }

to use the class:

        [Test]
        public void CanConnectToTestTFSRepository_AndRetreiveWorkItems()
        {
            const string someQuery =
                "SELECT System.Id,System.CreatedDate,System.Title FROM WorkItems WHERE System.CreatedDate >= '1/9/2008'";
            const string testTfsUrl = "https://tfsserver";
            const string testProject = "ProjectName";
            var testCredential = new NetworkCredential("ActiveDirUsername", "ActiveDirPassword", "ActiveDirDomain");

            IList storyCards = new WorkItemRepository().GetWorkItems(
                testTfsUrl,
                testProject,
                someQuery,
                testCredential);

            Assert.IsNotNull(storyCards, "failed to retreive any work item");
            Assert.IsTrue(storyCards.Count > 0, "no work item found");
        }

tested on Visual Studio 2008 .Net 3.5 SP1 without team explorer installed

  1. says:

    i feel lucky can find this usefull informations..

    thanks for this great posting..