Share via


How to: Edit and Save a WorkItem

You can view and edit the individual Fields, Links, and Attachments of a WorkItem. As soon as you make any edits to these parts of a WorkItem, the IsDirty property marks the WorkItem as dirty.

You can commit changes to a WorkItem to the server with the Save method. Unless you save a work item, you will lose all changes to the WorkItem.

During a save, the rules defined for the WorkItemType are evaluated. Violations will throw exceptions upon saving. If there are no violations, the WorkItem saves successfully, incrementing its revision, and updating its history with the latest changes.

Note

You can use the IsValid method to check whether a WorkItem's Fields pass all rules before you try to save the WorkItem.

Example

The sample performs the following

  • Connects to the Team Foundation Server and the WorkItemStore on that server.

  • Gets a specific WorkItem from the WorkItemStore.

  • Saves the WorkItem's Priority so it can be restored later.

  • Sets the WorkItem's Priority to an arbitrarily high number. Under typical circumstances, this will cause IsValid to be false for the WorkItem and the Priority Field.

  • Displays a message to indicate that the IsDirty flag of the WorkItem is true.

  • Tries to save the invalid WorkItem. If the WorkItem is invalid, an exception is thrown and an error message is displayed.

  • Sets the Priority to 1 or 2, whichever is different from the WorkItem's old value.

  • If the WorkItem is valid, saves the changed. WorkItem.

  • Restores the WorkItem's Priority to its original value, then saves the WorkItem again.

The following assemblies are required to compile the sample

Microsoft.TeamFoundation.dll

Microsoft.TeamFoundation.Client.dll

Microsoft.TeamFoundation.Common.dll

Microsoft.TeamFoundation.WorkItemTracking.Client.dll

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;


namespace WorkItemTrackingSample2
{
    class Program
    {
        static void Main(string[] args)
        {
            // Connect to the server and the store.
            TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer("YourTfsServerNameHere");
            WorkItemStore workItemStore = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));

            // Get a specific WorkItem from the store.
            //   Replace "12345" with a WorkItem ID appropriate for testing.
            WorkItem workItem = workItemStore.GetWorkItem(12345);

            // Save the existing Priority so we can restore it later.
            int oldPriority = (int)workItem.Fields["Priority"].Value;

            // Set the Priority to an arbitrarily high number.
            workItem.Fields["Priority"].Value = 9999;

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

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

            if (workItem.Fields["Priority"].IsValid == false)
                Console.WriteLine("The workItem's Priority field is not valid.");

            // Tries to save the invalid WorkItem.
            try
            {
                workItem.Save();
            }
            catch (ValidationException)
            {
                Console.WriteLine("The workItem threw a ValidationException.");
            }

            // Set the priority to a more reasonable number.
            if (oldPriority == 1)
                workItem.Fields["Priority"].Value = 2;
            else
                workItem.Fields["Priority"].Value = 1;

            // If the WorkItem is valid, saves the changed WorkItem.
            if (workItem.IsValid())
            {
                workItem.Save();
                Console.WriteLine("The workItem saved this time.");
            }

            // Restore the WorkItem's Priority to its original value.
            workItem.Fields["Priority"].Value = oldPriority;
            workItem.Save();
        }
    }
}

See Also

Concepts

Essentials of Work Item Tracking Object Model

Work Item Tracking Architecture