Blue
Blue

Reputation: 310

Azure DevOps Get work item notification using API

I want to make a listener that fires an event when any work item gets added/updated/deleted etc.

Current code

using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.Notification;
using Microsoft.VisualStudio.Services.WebApi;
using System;

namespace DevOpsApiTest
{
    class Connector
    {
        public void ConnectToDevOps()
        {
            try
            {
                Uri uri = new Uri("https://dev.azure.com/Org");
                VssCredentials creds = new VssBasicCredential("Username", "Password");
                VssConnection connection = new VssConnection(uri, creds);

                WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();
                WorkItem workitem = witClient.GetWorkItemAsync("TestProject", 1).Result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

How to make a listener that fires an event when I add/update/delete work item on the DevOps project?

Upvotes: 1

Views: 822

Answers (1)

danielorn
danielorn

Reputation: 6147

You can utilize a service hook

Service hook publishers define a set of events. Subscriptions listen for the events and define actions to take based on the event. Subscriptions also target consumers, which are external services that can run their own actions, when an event occurs.

You can start looking into web hooks, which is easy to get started with, provided that you can publish your piece of code above on a publicly accessible url

Webhooks provide a way to send a JSON representation of an event to any service. All that is required is a public endpoint (HTTP or HTTPS).

The is configured in your project settings and you can configure three of them to fire events on work item created, work item updated and work item deleted respectively.

The web hook sends a HTTP request to the endpoint you specify with a json payload containing information about the event. You would have to modify your code to act as a server accepting those requests instead of being a client.

Upvotes: 1

Related Questions