Share via


Team Foundation용 클라이언트 개체 모델을 사용하여 작업 항목 편집 및 저장

변경할 수는 Fields, Links, 및 AttachmentsWorkItem 사용 하 여 변경 내용을 저장 하려고는 WorkItem.Save 또는 WorkItemStore.BatchSave 메서드.

변경 내용을 저장 하려고 하면 규칙에 대해 평가 되는 WorkItemType. 지정 하는 값을 이러한 규칙을 따르는 경우는 WorkItem 는 저장 하 고, 해당 개정 증가 하 고 기록을 최신 변경 내용으로 업데이트 됩니다. 그렇지 않은 경우는 WorkItem 는 저장 하지 및 해당 버전이 증가 하지 않습니다, 그 기록이 업데이트 되지.

참고

두 개 이상 저장할 수 있습니다 WorkItem 또는 WorkItemLink 를 사용 하 여 단일 라운드트립에서 WorkItemStore.BatchSave 메서드.

예제

편집 하 고 작업 항목을 저장 하는 방법 및 사용 방법 예제는 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용 클라이언트 개체 모델을 사용하여 작업 항목 추적 확장