开发者

How to update a custom TFS field programmatically

开发者 https://www.devze.com 2023-02-08 20:19 出处:网络
We have a custom build process (not using MS Build) and during that process I am adding a \"fake\" build to the globa开发者_高级运维l builds list. The reason I am doing that is so that you can select

We have a custom build process (not using MS Build) and during that process I am adding a "fake" build to the globa开发者_高级运维l builds list. The reason I am doing that is so that you can select the build for a given work item (found in build). We have a custom field, build included, which is intended to show which build that work item was fixed in. I am having trouble figuring out how to update this field programmatically. The idea is I will have a small app that does this that I will call during the build process, finding all work items since the last build, then updating the field for those work items. Any ideas?


Something like this should work for you:

public void UpdateTFSValue(string tfsServerUrl, string fieldToUpdate, 
   string valueToUpdateTo, int workItemID)
{   
    // Connect to the TFS Server
    TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(tfsUri));
    // Connect to the store of work items.
    _store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
    // Grab the work item we want to update
    WorkItem workItem = _store.GetWorkItem(workItemId);
    // Open it up for editing.  (Sometimes PartialOpen() works too and takes less time.)
    workItem.Open();
    // Update the field.
    workItem.Fields[fieldToUpdate] = valueToUpdateTo;
    // Save your changes.  If there is a constraint on the field and your value does not 
    // meet it then this save will fail. (Throw an exception.)  I leave that to you to
    // deal with as you see fit.
    workItem.Save();    
}

An example of calling this would be:

UpdateTFSValue("http://tfs2010dev:8080/tfs", "Integration Build", "Build Name", 1234);

The variable fieldToUpdate should be the name of the field, not the refname (ie. Integration Build, not Microsoft.VSTS.Build.IntegrationBuild)

You could probably get away with using PartialOpen(), but I am not sure.

You will probably need to add Microsoft.TeamFoundation.Client to your project. (And maybe Microsoft.TeamFoundation.Common)


This has changed for TFS 2012, basicly you have to add workItem.Fields[fieldToUpdate].Value

Updated Version of what @Vaccano wrote.

public void UpdateTFSValue(string tfsServerUrl, string fieldToUpdate, 
   string valueToUpdateTo, int workItemID)
{   
    // Connect to the TFS Server
    TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(tfsUri));
    // Connect to the store of work items.
    _store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
    // Grab the work item we want to update
    WorkItem workItem = _store.GetWorkItem(workItemId);
    // Open it up for editing.  (Sometimes PartialOpen() works too and takes less time.)
    workItem.Open();
    // Update the field.
    workItem.Fields[fieldToUpdate].Value = valueToUpdateTo;
    // Save your changes.  If there is a constraint on the field and your value does not 
    // meet it then this save will fail. (Throw an exception.)  I leave that to you to
    // deal with as you see fit.
    workItem.Save();    
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号