Condividi tramite


Creare un elemento di lavoro tramite il modello a oggetti del client per Team Foundation

È possibile creare bug, attività e altri tipi di oggetti WorkItem eseguendo i passaggi seguenti:

  1. Creare un elemento WorkItem.

  2. Impostare i valori dei campi obbligatori.

  3. Salvare l'elemento WorkItem.

Esempio

A seconda del tipo di elemento WorkItem che si crea, la maggior parte degli elementi Fields presentano valori predefiniti. Se questi valori sono corretti, non è necessario impostarli in modo esplicito. Ad esempio, è possibile creare una storia utente come descritto in MSF for Agile Software Development per Visual Studio ALM. Per questo tipo di elemento WorkItem, i Fields Stato, Motivo e Assegnato a sono tutti obbligatori ma presentano valori predefiniti. Quando una storia utente viene creata, il relativo stato predefinito è "Attivo" il motivo predefinito è "Nuovo" e il valore predefinito del campo Assegnato a è l'utente corrente. Tuttavia, il titolo è obbligatorio e non ha un valore predefinito. È quindi necessario impostare il titolo quando si crea una storia utente. Per altre informazioni, vedere Storia utente (Agile) [reindirizzato] e Visualizzazione end-to-end di ciò che è possibile configurare e personalizzare in Visual Studio TFS. Nell'esempio seguente viene creata una storia utente, viene impostato il titolo, che è obbligatorio, e viene impostata la descrizione, anche se non necessaria.

Storia utente

Per usare questo esempio

  1. Creare un'applicazione console in C# o in VB.

  2. Aggiungere riferimenti agli assembly riportati di seguito:

  3. Sostituire il contenuto di Program.cs (o di Module1.vb) con questo esempio.

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

namespace WorkItemTrackingSample
{
    class Program
    {
        static void Main(string[] args)
        {            // Connect to the server and the store, and get the WorkItemType object
            // for user stories from the team project where the user story will be created. 
            Uri collectionUri = (args.Length < 1) ?
                new Uri("http://server:port/vdir/DefaultCollection") : new Uri(args[0]);
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(collectionUri);
            WorkItemStore workItemStore = tpc.GetService<WorkItemStore>();
            Project teamProject = workItemStore.Projects["DinnerNow"];
            WorkItemType workItemType = teamProject.WorkItemTypes["User Story"];

            // Create the work item. 
            WorkItem userStory = new WorkItem(workItemType)
            {
                // The title is generally the only required field that doesn’t have a default value. 
                // You must set it, or you can’t save the work item. If you’re working with another
                // type of work item, there may be other fields that you’ll have to set.
                Title = "Recently ordered menu",
                Description =
                    "As a return customer, I want to see items that I've recently ordered."
            };

            // Save the new user story. 
            userStory.Save();
        }
    }
}
Imports System
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Module Module1

    Sub Main(ByVal sArgs() As String)
        ' Connect to the server and the store and get the WorkItemType object
        ' for user stories from the team project where the user story will be created.

        Dim collectionUri As Uri
        If sArgs.Length = 0 Then
            collectionUri = New Uri("https://Server:8080/tfs/DefaultCollection")
        Else
            collectionUri = New Uri(sArgs(1))
        End If

        Dim tpc As New TfsTeamProjectCollection(collectionUri)
        Dim workItemStore As WorkItemStore
        workItemStore = tpc.GetService(Of WorkItemStore)()
        Dim teamProject As Project
        teamProject = workItemStore.Projects("DinnerNow")
        Dim workItemType As WorkItemType
        workItemType = teamProject.WorkItemTypes("User Story")

        ' Create the work item
        Dim userStory As New Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem(workItemType)

       ' The title is generally the only required field that doesn’t have a default value. 
       ' You must set it, or you can’t save the work item. If you’re working with another
       ' type of work item, there may be other fields that you’ll have to set.
        userStory.Title = "Recently Ordered Menu"
        userStory.Description = "As a return customer, I want to see items that I've recently ordered"

        ' Save the new user story
        userStory.Save()



    End Sub

End Module

Nota

È possibile salvare più di un elemento WorkItem o WorkItemLink in un unico round trip mediante il metodo WorkItemStore.BatchSave.

Vedere anche

Attività

Modificare e salvare elementi di lavoro tramite il modello a oggetti del client per Team Foundation

Riferimenti

BatchSave

Concetti

Estensione della gestione degli elementi di lavoro tramite il modello a oggetti del client per Team Foundation

Scrittura di codice per diversi tipi di elementi di lavoro tramite il modello a oggetti del client per Team Foundation