How to update SharePoint List Webhook Subscription Expiration Date using Azure Timer Trigger Function

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

When we use SharePoint List Webhook, one of the major concerns is to keep track on ExpirationDate. According to this docs from Microsoft, the expiration date should not be more than 180 days. So we need to update Expiration date before it gets expired.

There are various ways to automate this. Below we use Azure Time Trigger function which executes on particular interval and it will update Expiration date,

Step 1: Creating new Azure Function using VS 2017.

Step 2: Select Timer Trigger project and provide value in schedule which is a cron expression.

{second} {minute} {hour} {day} {month} {day of week}

Below you can find min-max value, * means any value between min-max

{0-59} {0-59} {0-23} {1-31} {1-12} {0-6}

By default it has 0 */5 * * * * which means it runs every 5 minutes and I want to run this every 1st day of every month, so the cron expression will be * * * 1 * * and this will execute 1st of every month (for testing, we can have small cron value (maybe every 30 sec or 1 min)).

Step 3: In the previous article – Update SharePoint List Webhook Subscription using PnP – C# programmatically by Custom Assembly we created a Class Library project which will update SP List Webhook subscription and we refer that assembly in this Azure function project.

Step 4: Paste the below code in Run function, which takes care of updating SP List Webhook subscription Expiration date.

 using System;
 using CustomListWebhookAssembly;
 using Microsoft.Azure.WebJobs;
 using Microsoft.Azure.WebJobs.Host;
 
 namespace AzTimer
 {
     public static class Function1
     {
         [FunctionName("Function1")]
         public static void Run([TimerTrigger("* * * 1 * *")]TimerInfo myTimer, TraceWriter log)
         {
             log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
             #region CustomListWebhookAssembly Area
             SubscriptionInfo subInfo = new SubscriptionInfo
             {
                 ClientID = "3xxxxc-6xxb-43c8-bxxb-2fxxxxxxbb",
                 ClientSecretKey = "xxxxxxxxsaCWQlnxxxxxxxxxLhk=",
                 ListGUID = "c5xxxx8-287e-4xx1-xx58-6fxxxxxc731a",
                 SubscriptionGUID = "29xxxxa-9xxd-xx5d-9xxe-33xxxxxx04e",
                 SiteURL = "https://fazildev.sharepoint.com/sites/sppals",
                 EndpointURL = "https://my-azure-fn.azurewebsites.net/api/SPListWebhook",
                 //Provide new Expiration Date
                 ExpirationDateTime = DateTime.Today.AddDays(179)
             };
             WebhookOperation.UpdateSubscription(subInfo);
             #endregion
         }
     }
 }
 

Step 5: Publish the Az function.

To avoid deleting existing files. Please uncheck Remove additional files at destination to avoid deleting existing files in your Azure function app 😉

 

Happy Coding

Ahamed

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