Create a service hook subscription programmatically
Raksts
Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019
Using the Subscriptions REST APIs , you can programmatically create a subscription that performs an action on an external/consumer service when a specific event occurs in an Azure DevOps project. For example, you can create a subscription to notify your service when a build fails.
Supported events:
Build completed
Code pushed (for Git projects)
Pull request create or updated (for Git projects)
Code checked in (TFVC projects)
Work item created, updated, deleted, restored or commented on
You can configure filters on your subscriptions to control which events trigger an action. For example, you can filter the build completed event based on the build status. For a complete set of supported events and filter options, see the Event reference.
For a complete set of supported consumer services and actions, see the Consumer reference.
Construct the body of the HTTP POST request to create the subscription based on the project ID, event, consumer, and action.
See the following example request for creating a subscription that causes a build event to POST to https://myservice/event when the build WebSite.CI fails.
If the subscription request fails, you get an HTTP response code of 400 with a message that has further details.
What happens when the event occurs?
When an event occurs, all enabled subscriptions in the project are evaluated, and the consumer action is performed for all matching subscriptions.
Resource versions (advanced)
Resource versioning is applicable when an API is in preview. For most scenarios, specifying 1.0 as the resource version is the safest route.
The event payload sent to certain consumers, like Webhooks, Azure Service Bus, and Azure Storage, includes a JSON representation of subject resource (for example, a build or work item). The representation of this resource can have different forms or versions.
You can specify the version of the resource that you want to have sent to the consumer service via the resourceVersion field on the subscription.
The resource version is the same as the API version. Not specifying a resource version means "latest released". You should always specify a resource version, which ensures a consistent event payload over time.
FAQs
Q: Are there services that I can subscribe to manually?
A: Yes. For more information about the services that you can subscribe to from the administration page for a project, see the Overview.
Q: Are there C# libraries that I can use to create subscriptions?
A: No, but here's a sample to help you get started.
cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Mvc;
namespaceMicrosoft.Samples.VisualStudioOnline
{
publicclassServiceHookEventController : Controller
{
// POST: /ServiceHookEvent/workitemcreated
[HttpPost]
public HttpResponseMessage WorkItemCreated(Content workItemEvent)
{
//Grabbing the title for the new workitemvarvalue = RetrieveFieldValue("System.field", workItemEvent.Resource.Fields);
//Acknowledge event receiptreturnnew HttpResponseMessage(HttpStatusCode.OK);
}
///<summary>/// Gets the value for a specified work item field.///</summary>///<param name="key">Key used to retrieve matching value</param>///<param name="fields">List of fields for a work item</param>///<returns></returns>public String RetrieveFieldValue(String key, IList<FieldInfo> fields)
{
if (String.IsNullOrEmpty(key))
return String.Empty;
var result = fields.Single(s => s.Field.RefName == key);
return result.Value;
}
}
publicclassContent
{
public String SubscriptionId { get; set; }
publicint NotificationId { get; set; }
public String EventType { get; set; }
public WorkItemResource Resource { get; set; }
}
publicclassWorkItemResource
{
public String UpdatesUrl { get; set; }
public IList<FieldInfo> Fields { get; set;}
publicint Id { get; set; }
publicint Rev { get; set; }
public String Url { get; set; }
public String WebUrl { get; set; }
}
publicclassFieldInfo
{
public FieldDetailedInfo Field { get; set; }
public String Value { get; set; }
}
publicclassFieldDetailedInfo
{
publicint Id { get; set; }
public String Name { get; set; }
public String RefName { get; set; }
}
}
Pievienojieties meetup sērijai, lai kopā ar citiem izstrādātājiem un ekspertiem izveidotu mērogojamus AI risinājumus, kuru pamatā ir reālas lietošanas gadījumi.