Update SharePoint List Webhook Subscription using PnP – C# programmatically

Ahamed Fazil Buhari
 
Senior Developer
April 17, 2019
 
Rate this article
 
Views
2162

There are different ways to update SharePoint List Webhook Subscription using PostMan, PnP PowerShell, C#, JS. In this article we will see how to update SP List Webhook subscription using C#.

In the below code we created a simple Class Library project to implement SP List Webhook operations (in the upcoming article we will use this assembly in Azure function with Timer). It’s not necessary to have Class Library project to implement, we can even directly use the methods available here.

And add the SharePointPnPCoreOnline from NuGet Package Manager as shown below,

Created WebhookOperation.cs class

 public class WebhookOperation
 {
 	public static void UpdateSubscription(SubscriptionInfo subscription)
 	{
 		string siteUrl = subscription.SiteURL;
 		string clientId = subscription.ClientID;
 		string clientSecret = subscription.ClientSecretKey;
 		Guid listID = new Guid(subscription.ListGUID);
 		Guid subscriptionID = new Guid(subscription.SubscriptionGUID);
 		using (var ctx = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret))
 		{
 			try
 			{
 				List targetList = ctx.Web.Lists.GetById(listID);
 				bool isUpdated = ListExtensions.UpdateWebhookSubscription(targetList, subscriptionID, subscription.EndpointURL, subscription.ExpirationDateTime);
 				if (isUpdated)
 				{
 					Console.WriteLine("Success");
 				}
 			}
 			catch (Exception ex)
 			{
 				Console.WriteLine("Error", ex.Message);
 			}
 		}
 	}
 }

In Model.cs file, just defined the property that will be used in Webhook Operation class

 public class SubscriptionInfo
 {
 	public string SiteURL { get; set; }
 	public string ListGUID { get; set; }
 	public string SubscriptionGUID { get; set; }
 	public string ClientID { get; set; }
 	public string ClientSecretKey { get; set; }
 	public string EndpointURL { get; set; }
 	public DateTime ExpirationDateTime { get; set; }
 }

Created a simple Console Application to test the class library project which is created above,

And added the reference

 using CustomListWebhookAssembly;
 using System;
 
 namespace TestingConsoleApp
 {
     class Program
     {
         static void Main(string[] args)
         {
             #region CustomListWebhookAssembly Area
             SubscriptionInfo psmSubInfo = new SubscriptionInfo
             {
                 ClientID = "3xx-664b-43c8-b2fb-2fxxxxxb",
                 ClientSecretKey = "Gxx/OxxTxxxxle8Lhk=",
                 ListGUID = "c5xx28-287e-4x2x-b9xx8-6f2xxxxxxxa",
                 SubscriptionGUID = "297fcc6a-9xxx-415d-9dee-33xxxxxx4e",
                 SiteURL = "https://fazildev.sharepoint.com/sites/sppals",
                 EndpointURL = "https://fazilazure-sp-sppals-test.azurewebsites.net/api/MyWebhook",
   //Provide new Expiration Date – Should not be more than 180 days
                 ExpirationDateTime = DateTime.Today.AddDays(12)
             };
             WebhookOperation.UpdateSubscription(psmSubInfo);
             #endregion
         }
     }
 }
 

To get SubscriptionGUID and other information, use below PowerShell and I already subscribed to SharePoint list webhook.

In the upcoming article, we will see how to include this custom assembly in Azure function and update ExpirationDateTime using timer function.

Happy Coding

Ahamed

Category : CSOM, PNP, SharePoint

Author Info

Ahamed Fazil Buhari
 
Senior Developer
 
Rate this article
 
Ahamed is a Senior Developer and he has very good experience in the field of Microsoft Technologies, especially SharePoint, Azure, M365, SPFx, .NET and client side scripting - JavaScript, TypeScript, ...read more
 

Leave a comment