This documentation is archived and is not being maintained.

Edit and Save Work Items by Using the Client Object Model for Team Foundation

You can change the Fields, Links, and Attachments of a WorkItem and then try to save those changes by using either the WorkItemSave or WorkItemStoreBatchSave method.

When you try to save your changes, they are evaluated against the rules for the WorkItemType. If the values that you specify follow those rules, the WorkItem is saved, its revision is incremented, and its history is updated with the most recent changes. Otherwise, the WorkItem is not saved, its revision is not incremented, and its history is not updated.

Note Note

You can save more than one WorkItem or WorkItemLink in a single round trip by using the WorkItemStoreBatchSave method.

The example demonstrates how to edit and save work items and how to use the WorkItemIsValid and WorkItemIsDirty properties.

To use this example

  1. Create a C# ( or VB ) console application.

  2. Add references to the following assemblies:

  3. Replace the contents of Program.cs ( or Module1.vb ) with the following example:

using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace WorkItemTrackingSample
    class Program
        static void Main(string[] args)
            Uri collectionUri = (args.Length < 1) ?
                new Uri("http://server:port/vdir/DefaultCollection") : new Uri(args[0]);

            // Connect to the server and the store. 
            TfsTeamProjectCollection teamProjectCollection =
               new TfsTeamProjectCollection(collectionUri);

            WorkItemStore workItemStore = teamProjectCollection.GetService<WorkItemStore>();

            // Get a specific work item from the store. (In this case, 
            // get the work item with ID=1.) 
            WorkItem workItem = workItemStore.GetWorkItem(1);

            // Set the value of a field to one that is not valid, and save the old
            // value so that you can restore it later. 
            string oldAssignedTo = (string)workItem.Fields["Assigned to"].Value;
            workItem.Fields["Assigned to"].Value = "Not a valid user";

            // Display the results of this change. 
            if (workItem.IsDirty)
                Console.WriteLine("The work item has changed but has not been saved.");

            if (workItem.IsValid() == false)
                Console.WriteLine("The work item is not valid.");

            if (workItem.Fields["Assigned to"].IsValid == false)
                Console.WriteLine("The value of the Assigned to field is not valid.");

            // Try to save the work item while it is not valid, and catch the exception. 
            catch (ValidationException exception)
                Console.WriteLine("The work item threw a validation exception.");

            // Set the state to a valid value that is not the old value. 
            workItem.Fields["Assigned to"].Value = "ValidUser";

            // If the work item is valid, save the changes. 
            if (workItem.IsValid())
                Console.WriteLine("The work item was saved this time.");

            // Restore the original value of the work item's Assigned to field, and save that change. 
            workItem.Fields["Assigned to"].Value = oldAssignedTo;