Home > Systems development using .Net > How to get files associated to specified work item from TFS

How to get files associated to specified work item from TFS

Today I’ve got a question on how to get files that are associated with specific work item. The requirement behind that was to grab configuration, data and similar files that were associated to a specific change request, which was entered as a work item.
After quick binging and browsing of the Team Foundation Server SDK, I’ve got the sample below.

static void Main(string[] args)
    using (TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(args[0],
        new UICredentialsProvider()))
        WorkItemStore wiStore = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
        VersionControlServer vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));

        int workItemId;
        if (int.TryParse(args[1], out workItemId))
            WorkItem workItem = wiStore.GetWorkItem(workItemId);
            Console.WriteLine("Work item: {0}", workItem.Title);
            //We look for links associated with work item
            foreach (Link link in workItem.Links)
                ExternalLink extLink = link as ExternalLink;
                if (extLink != null)
                    ArtifactId artifact = LinkingUtilities.DecodeUri(extLink.LinkedArtifactUri);
                    //For this example I grab Changeset directly
                    //however in the real scenario you could grab other related workitems
                    //and this way parse entire tree up to the changeset
                    if (String.Equals(artifact.ArtifactType, "Changeset", StringComparison.Ordinal))
                        // Convert the artifact URI to Changeset object.
                        Changeset cs = vcs.ArtifactProvider.GetChangeset(new Uri(extLink.LinkedArtifactUri));
                        foreach (Change change in cs.Changes)
                            //We want to download files only
                            if (change.Item.ItemType == ItemType.File)

private static void RetrieveFile(Change change)
    //Technically we should process the information
    //i.e. you should create separate folders according to the server location
    //to avoid the file overwriting
    //if folders are not present - those should be created
    string fileName = change.Item.ServerItem.Split('/').Last();
    Console.WriteLine("Item name: {0}. Last operation {1}", fileName, change.ChangeType);
    change.Item.DownloadFile(@"C:MyProjectsVS10SolutionGetWIFilesttt" + fileName);

Here go the credits: http://blogs.msdn.com/buckh/archive/2006/08/12/artifact_uri_to_changeset.aspx
A quick reminder to myself (and the others): TFS is VERY extensible. You can extend the processes, work items, reports, functionality, … just need the right tools

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 114 other followers

%d bloggers like this: