Create a new work item in Azure DevOps using python api

Overview:

Rohit Singh
2 min readJan 19, 2024

The azure devops python api can be used to programmatically create work items in azure devops. This has real work significance in scenarios where one needs to create work items in bulk such as when migrating from other project management software's or creating work items from data in excel sheets.

In the snippet below, we have a covered a basic program in python which creates a new work item in azure devops using the python api. As it seems obvious, along with the python api, we also need an azure devops project as a pre-requisite. There are few inputs required when interacting with azure devops programmatically with the python api described as below.

Azure DevOps Organization Url: The variable “organization_url” in the code snipped below is used to provide the azdo organization url.

PAT Token: The variable “personal_access_token” in the code snipped below is used to provide thepat token to authenticate to the azdo project.

Project Name: The variable “project_name” in the code snipped below is used to provide the azdo project name.

Work Item Type: The variable “work_item_type” in the code snipped below is used to provide the type of the work item which would be created.

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v7_1.work_item_tracking.models import JsonPatchOperation

# Your Azure DevOps organization URL and personal access token
organization_url = ""
personal_access_token = ""
project_name = ""
work_item_type = ""

# Create a connection to Azure DevOps
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
wit_client = connection.clients.get_work_item_tracking_client()

# Work item data
work_item_data = [
JsonPatchOperation(
op="add",
path="/fields/System.Title",
value="ADO Task via Python API"
),
]

created_work_item = wit_client.create_work_item(
document=work_item_data,
project=project_name,
type=work_item_type
)

print(f"Work item created with ID: {created_work_item.id}")

Description:

Now, lets take a look at the code itself to understand in detail how it works by breaking down the program in functional steps.

Create a connection to azure devops: In this step a connection to azdo is established and then a work item tracking client is created as specified in the code below.

credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
wit_client = connection.clients.get_work_item_tracking_client()

Prepare the work item data/document: In this we basically prepare the work item data that would be created. In the example below, the work item’s title is set using the path and value variables.

# Work item data
work_item_data = [
JsonPatchOperation(
op="add",
path="/fields/System.Title",
value="ADO Task via Python API"
),
]

We could also add multiple fields and also dynamically pass the value for fields.

# Work item data
work_item_data = [
JsonPatchOperation(
op="add",
path="/fields/System.Title",
value="ADO Task via Python API"
),
op="add",
path="/fields/System.Description",
value=f"{YOUR_VARIABLE_HERE}" # Passing value dynamically
),
]

Create the work item: Finally, in this step we create the work item using the create_work_item() function and pass the work_item_data, project_name & work_item_type.

Conclusion: Hope this helps gaining an understanding on how python api for azure devops can be used to create a work item in azdo programatically.

--

--