使用 Team Foundation 的用戶端物件模型來編輯和儲存工作項目

您可以使用 WorkItem.SaveWorkItemStore.BatchSave 方法,您可以變更 FieldsWorkItemLinksAttachments 然後嘗試儲存這些變更。

當您嘗試儲存變更時,會被評估 WorkItemType的規則。 如果您指定的值遵循這些規則, WorkItem 儲存,其修訂會加入,然後,其記錄就會以最新的變更。 否則, WorkItem 不會儲存,它的修改不會遞增,,其記錄就不會更新。

注意事項注意事項

您可以使用 WorkItemStore.BatchSave 方法,您可以將多個 WorkItemWorkItemLink 在單一來回行程。

範例

範例會示範如何編輯並且儲存工作項目以及如何使用 WorkItem.IsValidWorkItem.IsDirty 屬性。

使用這個範例

  1. 建立 C# (或 VB) 主控台應用程式。

  2. 加入下列組件的參考:

  3. 以下列範例取代 Program.cs (或 Module1.vb) 內容:

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. 
            try
            {
                workItem.Save();
            }
            catch (ValidationException exception)
            {
                Console.WriteLine("The work item threw a validation exception.");
                Console.WriteLine(exception.Message);
            }

            // 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())
            {
                workItem.Save();
                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;
            workItem.Save();
        }
    }
}
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

請參閱

概念

使用 Team Foundation 的用戶端物件模型來建立工作項目

使用 Team Foundation 的用戶端物件模型,為不同的工作項目類型撰寫程式碼

使用 Team Foundation 的用戶端物件模型,擴充工作項目追蹤的功能