Create vsts work item with Azure DevOps REST API

Sandeep Singh
3 min readJul 18, 2019

--

Photo by John Schnobrich on Unsplash

Azure Work Item provide rest API to create task, product backlog items in Azure DevOps. We can integrate this rest API with external systems to automate the flow to create a work item for production failures etc.

The Azure DevOps documentation has provided the sample details to create work item however it is missing the crucial details like how to provide the:

Task/PBI Description (Task or Product Backlog Item description).
Task/PBI Assigned (Whom you want to assign this task to look into).
Task/PBI Comment (Comment you want to put while creating the task).
Task/PBI Acceptance Criteria (What should be the acceptance criteria).
Task/PBI Project Area (Respective Group/Team).

In this post I will be discussing the required steps to create a work item with all the required details.

Firstly, I will use the provided sample api and request body json to create a work item:

Sample Request:

HTTPPOST https://dev.azure.com/{project}/_apis/wit/workitems/${type}?api-version=5.1

Note: Make sure,do not lost $ before {type}, such as: If you want to add task, please refer this sample:

/_apis/wit/workitems/$task?api-version=5.1-preview.3

Sample Request Body:

[
{
“op”: “add”,
“path”: “/fields/System.Title”,
“from”: null,
“value”: “Sample task”
}
]

Problem:

Since the sample request body has fields “Title” only, it will create task with title only and all other information such as assigned to, description and project area etc will be remain empty. If we want to include missing details, we need to include other fields along with the request body such as:

System.Title → For Title
System.AreaPath → For project team
System.AssignedTo → Assigned person
System.History → Comment
System.Description → Task Description

Once we include these fields along with the request body, we can be able to create task along with the provided information.

Sample Request Body:

[
{
“op”: “add”,
“path”: “/fields/System.Title”,
“from”: null,
“value”: “Sample Task”
},
{
“op”: “add”,
“path”: “/fields/System.Description”,
“from”: null,
“value”: “Sample task failed due to error”
},
{
“op”: “add”,
“path”: “/fields/System.History”,
“from”: null,
“value”: “Test Comment”
},
{
“op”: “add”,
“path”: “/fields/System.AssignedTo”,
“from”: null,
“value”: “SKumar30@myorg.com
},
{
“op”: “add”,
“path”: “/fields/System.AreaPath”,
“from”: null,
“value”: “data-development\\Big Data Team”
}

]

If you want to add Product Backlog Item, please refer this sample:

/_apis/wit/workitems/$Product Backlog Item?api-version=5.1-preview.3

Sample Request Body:

[
{
“op”: “add”,
“path”: “/fields/System.Title”,
“from”: null,
“value”: “Sample PBI 2”
},
{
“op”: “add”,
“path”: “/fields/System.Description”,
“from”: null,
“value”: “Sample task failed due to error”
},
{
“op”: “add”,
“path”: “/fields/System.History”,
“from”: null,
“value”: “Test comment”
},
{
“op”: “add”,
“path”: “/fields/System.AssignedTo”,
“from”: null,
“value”: “SKumar30@myorg.com
},
{
“op”: “add”,
“path”: “/fields/System.AreaPath”,
“from”: null,
“value”: “data-development\\Big Data Team”
},
{
“op”: “add”,
“path”: “/fields/Microsoft.VSTS.Common.AcceptanceCriteria”,
“from”: null,
“value”: “Test Acceptance”
}

]

If you want to add more fields such as “Priority” etc, and want to know the respective key, use below sample URL to get the response from any valid existing task, observed the response body,you will find the key such as for priority:

“Microsoft.VSTS.Common.Priority”: 2

Now add it in request body while sending the request to create the task, you will have a task created with priority 2.

{
“op”: “add”,
“path”: “/fields/Microsoft.VSTS.Common.Priority”,
“from”: null,
“value”: “2”
}

Sample URL to get the response:

GET https://dev-it.azure.com/{project}/_apis/wit/workitems/{id}?api-version=5.1-preview.3

Example:

GET https://dev-it.azure.com/{project}/_apis/wit/workitems/694646?api-version=5.1-preview.3

Prerequisites:

1. Make sure you have create permission in your project area.
2. Make sure you have authenticated and rest API.

--

--