Programmatically getting workitems associated with given changsets - inverese queries

I was looking for a quick sample on linking queries, but was surprised that I couldn't find one. There is "collectibles" sample in VSSDK that covers a lot on linking, but it is huge and I think a one pager sample would be helpful for most needs. So, I am posting a sample here. Here is sample on getting workitems associated with a given changeset.

        private List<WorkItem> GetWorkItemsForChangeSet(string serverName, int changeSet)


            // Error handling code skipped for clarity.

            // Get TFS and store references.

            TeamFoundationServer server = TeamFoundationServerFactory.GetServer(serverName);

            WorkItemStore store = (WorkItemStore)server.GetService(typeof(WorkItemStore));

            ILinking linking = (ILinking)server.GetService(typeof(ILinking));

            // Get URI for changeset

            ArtifactId changeSetId = new ArtifactId();

            changeSetId.Tool = "VersionControl";

            changeSetId.ArtifactType = "ChangeSet";

            changeSetId.ToolSpecificId = changeSet.ToString();

            string changeSetUri = LinkingUtilities.EncodeUri(changeSetId);

  // Get referencing artifacts for given changeset

            Artifact[] artifacts = linking.GetReferencingArtifacts(new string[] { changeSetUri });

            List<WorkItem> workItems = new List<WorkItem>();

            foreach (Artifact artifact in artifacts)


                ArtifactId artifactId = LinkingUtilities.DecodeUri(artifact.Uri);

                if (String.Equals(artifactId.Tool, "WorkItemTracking", StringComparison.OrdinalIgnoreCase))


           WorkItem wi = store.GetWorkItem(Convert.ToInt32(artifactId.ToolSpecificId));




            return workItems;


Dll references needed:


using Microsoft.TeamFoundation.Client;

using Microsoft.TeamFoundation;

using Microsoft.TeamFoundation.WorkItemTracking.Client;


The sample shows use of GetReferencingArtifacts. GetArtifacts can be used with similar syntax. Multiple URIs can be passed to "bulk" get artifacts.

Where to get documentation for ILinking and related methods?

There is a good documentation on linking in VSSDK under folder "Visual Studio 2005 SDK\<version>\VisualStudioTeamSystemIntegration\Team Foundation Core Services\Team Foundation Linking Service.doc". The "Collecibles" sample there shows how to extend linking to add new linktypes.

How to query on workitems associated with version control files?

The difficulty here is getting the ToolSpecificId for version control files (such as source code files). I am not aware of a way in UI. We could get it using version control object model or by adding a source control item in WI and checking the ArtifactUri propery in WorkItem's Links collection .

How to do the reverse, getting changesets for a workitem?

For getting changesets for multiple items in one shot, GetArtifacts method can be used. ToolSpecificId for workitems is workitem id. For a single item, using WIT object model is easy as described in the blog