microsoft / azure-devops-dotnet-samples

.NET/C# samples for integrating with Azure DevOps Services and Azure DevOps Server
https://docs.microsoft.com/azure/devops/integrate
MIT License
519 stars 511 forks source link

Keep Getting Exception When Attempting to Create New Work Item #270

Closed ideoclickVanessa closed 4 years ago

ideoclickVanessa commented 4 years ago

I am running into the same exception as #153 and this StackOverflow item. Both the closed issue and the StackOverflow item are over a year old and don't provide a working example; therefore, I'm opening this issue.

First, I manually created a new Product Backlog Item via the UI. As you can see from this screenshot, its Area and Iteration Path is IdeoClick: image

I then attempted to create a new Product Backlog Item via this nuget package: <PackageReference Include="Microsoft.TeamFoundationServer.Client" Version="16.153.0" /> and this code:

        static async Task CreateWorkItemAsync(WorkItemTrackingHttpClient workItemClient)
        {
            Console.WriteLine("Creating new pbi...");
            Console.WriteLine();

            Console.WriteLine("Creating new json document...");
            Console.WriteLine();

            JsonPatchDocument json = VssJsonPatchDocumentFactory.ConstructJsonPatchDocument(Operation.Add, new Dictionary<string, object>
            {
                { "/fields/System.AreaPath", "IdeoClick" },
                { "/fields/System.IterationPath", "IdeoClick" },
                { "/fields/System.Title" , "This is a test pbi" },
            });

            Console.WriteLine("Sending request...");
            Console.WriteLine();

            WorkItem workItem = await workItemClient.CreateWorkItemAsync(json, projectName, "Product Backlog Item");

            Console.WriteLine($"PBI `{workItem.Id}` created.");
            Console.WriteLine();
        }

However, it keeps throwing this exception:

Microsoft.VisualStudio.Services.WebApi.VssServiceResponseException
  HResult=0x80131600
  Message=New work item updates must specify Area and Iteration node ids.
Parameter name: workItemUpdates
  Source=Microsoft.VisualStudio.Services.WebApi
  StackTrace:
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<HandleResponseAsync>d__53.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__51.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__47`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__28`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at AzureDevOps.Program.<CreateWorkItemAsync>d__5.MoveNext() in C:\Users\VanessaRussell\source\repos\Mine\AzureDevOps\AzureDevOps\Program.cs:line 129
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at AzureDevOps.Program.<Main>d__3.MoveNext() in C:\Users\VanessaRussell\source\repos\Mine\AzureDevOps\AzureDevOps\Program.cs:line 37
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at AzureDevOps.Program.<Main>(String[] args)

Inner Exception 1:
ArgumentException: New work item updates must specify Area and Iteration node ids.
Parameter name: workItemUpdates

Could you please provide insight into what I'm doing wrong and/or provide a working Create Work Item sample?

ideoclickVanessa commented 4 years ago

Apparently this code was the problem for some reason:

JsonPatchDocument json = VssJsonPatchDocumentFactory.ConstructJsonPatchDocument(Operation.Add, new Dictionary<string, object>
{
    { "/fields/System.AreaPath", "IdeoClick" },
    { "/fields/System.IterationPath", "IdeoClick" },
    { "/fields/System.Title" , "This is a test bug 2." },
});

Once I more manually created the JsonPatchDocument like this:

JsonPatchDocument json = new JsonPatchDocument();
json.Add(new JsonPatchOperation
{
    Operation = Operation.Add,
    Path = "/fields/System.AreaPath",
    Value = "IdeoClick",
});
json.Add(new JsonPatchOperation
{
    Operation = Operation.Add,
    Path = "/fields/System.IterationPath",
    Value = "IdeoClick",
});
json.Add(new JsonPatchOperation
{
    Operation = Operation.Add,
    Path = "/fields/System.Title",
    Value = "This is a test bug.",
});

The exception went away and the call was successful.