This is my code:
var personalAccessToken = "...";
var orgUrl = new Uri("https://dev.azure.com/my-org");
var teamProjectName = "myproj";
VssHttpMessageHandler.DefaultWebProxy = new WebProxy("localhost:18080");
VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));
// Create instance of WorkItemTrackingHttpClient using VssConnection
WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();
try
{
JsonPatchDocument patchDocument = new JsonPatchDocument();
//add fields and their values to your patch document
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/System.Title",
Value = "Test WorkItem"
}
);
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/Microsoft.VSTS.TCM.ReproSteps",
Value = ""
}
);
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/Microsoft.VSTS.Common.Priority",
Value = "1"
}
);
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/Microsoft.VSTS.Common.Severity",
Value = "2 - High"
}
);
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/Microsoft.VSTS.TCM.ReproSteps",
Value = "Test Repro Steps"
}
);
WorkItem result = witClient.CreateWorkItemAsync(patchDocument, teamProjectName, "Bug").Result;
Console.WriteLine("Bug Successfully Created: Bug #{0}", result.Id);
}
catch (AggregateException ex)
{
Console.WriteLine("Error creating bug: {0}", ex.InnerException.Message);
}
It creates successfully (and running the code again gives an error about the item already existing with that name).
However when I go into azure devops in the browser, I cannot find any trace of the work item I created anywhere.
Any idea why?
CodePudding user response:
Tried your code, based on my test, actually the work item is not created because you try to update the field with reference name'Microsoft.VSTS.TCM.ReproSteps'
multiple times.
I get the following error message when debugging :
"Error creating bug: VS403691: Update to work item -1 had two or more updates for field with reference name 'Microsoft.VSTS.TCM.ReproSteps'. A field cannot be updated more than once in the same update.
"
Once I removed one of the patchDocument
, the work item can be created successfully and I can see it in Azure DevOps work item list.
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/Microsoft.VSTS.TCM.ReproSteps",
Value = ""
}
);
In case you posted the wrong demo code, then please check your work item list area path and iteration path settings. If the work item query/list is based on a specific area path or iteration path which the new created work item is not included in, then you cannot see the new work item.
UPDATE:
We can get the list of work item fields used in a work item type by calling