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 WorkItem.Save or WorkItemStore.BatchSave 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 WorkItemStore.BatchSave method.

The example demonstrates how to edit and save work items and how to use the WorkItem.IsValid and WorkItem.IsDirty 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:

Imports System
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Module Module1

    Sub Main(ByVal sArgs() As String)
        Dim collectionUri As Uri
        If sArgs.Length = 0 Then
            collectionUri = New Uri("http://Server:port/vdir/DefaultCollection")
        Else
            collectionUri = New Uri(sArgs(1))
        End If

        ' Connect to the server and the store.
        Dim teamProjectCollection As New TfsTeamProjectCollection(collectionUri)

        ' Get a specific work item from the store. (In this case, 
        ' get the work item with ID=1.)
        Dim workItemStore As WorkItemStore
        workItemStore = teamProjectCollection.GetService(Of WorkItemStore)()

        Dim workItem As 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.
        Dim oldAssignedTo As String
        oldAssignedTo = workItem.Fields("Assigned To").Value
        workItem.Fields("Assigned to").Value = "Not a Valid User"

        ' Display the results of this change

        If (workItem.IsDirty) Then
            Console.WriteLine("The work item has changed but has not been saved.")
        End If

        If (workItem.IsValid() = False) Then
            Console.WriteLine("The work item is not valid.")
        End If

        If (workItem.Fields("Assigned to").IsValid = False) Then
            Console.WriteLine("The value of the Assigned to field is not valid.")
        End If

        ' Try to save the work item while it is not valid, and catch the exception.
        Try
            workItem.Save()
        Catch exception As ValidationException

        End Try

        ' 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()) Then
            workItem.Save()
            Console.WriteLine("The work item was saved this time.")
        End If

        ' Restore the original value of the work item's Assigned to field, and save that change
        workItem.Fields("Assigned to").Value = oldAssignedTo
        workItem.Save()

    End Sub

End Module
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft