How to programmatically update a custom TFS field

We have a custom build process (without using MS Build), and during this process I add a "fake" assembly to the list of global assemblies. The reason I do this is because you can select the assembly for this work item (found in the assembly). We have a custom field, an integrated assembly, which is designed to show in which assembly the work item was fixed. I am having trouble figuring out how to update this field programmatically. The idea is that I will have a small application that does this, which I will invoke during the assembly process, finding all the work items since the last build, and then updating the field for these work items. Any ideas?

+5
source share
2 answers

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 might be:

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

The variable fieldToUpdateshould be the name of the field, not refname (i.e. Integration Build, not Microsoft.VSTS.Build.IntegrationBuild)

You may be able to use PartialOpen () , but I'm not sure.

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

+13
source

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

An 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();    
}
+4
source

All Articles