How to Create a product backlog item using Azure Dev Ops Restful API
Published: 2023-07-05 02:17:35

Why? — Sharing some of the lessons with developer community on how to automate repeatable tasks with Azure Dev Ops Rest API.

You can apply this pattern to create work items, trigger builds, test cases etc.

IDE: Visual Studio Code

Tech Stack: C#, FlurlHttp, Azure Dev Ops Restful API

Prerequisites:

Account created in Azure Dev Ops: https://azure.microsoft.com/en-us/products/devops/?nav=min

API Reference : https://learn.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-7.1

Create a Team project with the name : DevOpsDemos with Version control as “Git” and Work item process as “Scrum”

Go to User settings >> Personal access tokens and generate a new token so that it can be used for accessing the api.

How the code is structed:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/59mdvcqkyt4i2sgo957t.png)


Step 1: Create a ASP.NET Core Minimal Web API with the following packages:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57fn2td68vlb9u0o0zvr.png)

Step 2: Update appsettings.json as follows:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/753wpxxjnp9kp1x0uo2l.png)



Step 3: Create a record with the name “AzureDevOpsSettings” to load the configuration from appsettings.json created in previous step.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vmwnk51efi2ygsqc63c.png)



Step 4: Create Azure Dev Ops Client Request class as follows:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5z5d148f16vna6zbk5vv.png)


Step 5: Create an Access Token Provider class with an interface as follows:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hcdb17jh9bh8mt4t8ssd.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8bs6hdzor2kzj62klpv.png)

Step 6:

Create a AzureDevOpsHttpClientAdapter class with an interface as follows:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izfa5fpr0t8ktj0ib31q.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shk56fik99td9rmsjuwm.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y23qmu8zt62bax34khqx.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/udt6ntqys2ucao6gcwg4.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/438se34jgrfu02jyo8ov.png)

Step 7:

Create an AzureDevOpsServicesManager class with an interface as follows:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4asvgf059430eisdj4rm.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kxqrhb2p83x4kovwgkok.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plpg9sk6m3kh1u5fs0an.png)

Step 8:

Create a ProductBacklog Controller with an HttpPost Attribute with 2 Parameters.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/us816ib23htjxchukrkb.png)


Step 9:

Now update the program.cs to wire everything up and dependencies are injected.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7e13tv2ijx6i6cwji1xk.png)


Step 10: Now run the app using dotnet run command.

Step 11:

Now we can use any http clients to send a request to http://localhost:5276/api/ProductBackLog?title=Test PBI&description=Test

Hope this helps.