Introduction
SharePoint Server provides native Timer Jobs that inherit SPJobDefinition class to run at regular intervals defined in the SPSchedule object. This way, we can create solutions that have to be iteratively run to process logic at regular intervals. However, in the case of SharePoint Online, Native Timer Jobs cannot be used.
Though we can use Windows Service/Task Scheduler to simulate Timer Jobs in SharePoint Online, it is still not a complete cloud solution. However, now we have the option to create pure cloud solutions for SharePoint Online Timer Jobs using Azure Function.
What are we going to do?
In this article, we will explore the process of simulating Timer Jobs for SharePoint Online using Azure Functions. The video recording of the demo is shared here
Requirement for Simulating Timer Job in SharePoint Online using Azure web jobs
We have a List named ‘SLA’ that contains action items and an associated SLA date. Every day if the SLA date for the action item has reached, we have to intimate the concerned person about the items whose SLA has reached. Since the logic to check for SLA has to run every day, we will create a timer job solutions using Azure Function. As shown below, there are two items that have reached the SLA of 6/17/2017.

The Azure function timer job will get the SharePoint Online List items that have reached the SLA(checks if the SLA date is today) and will send the mail to the concerned Service User.

Create Azure Function to work as a SharePoint Timer
We will create an Azure Function app that will contain the code that implements the logic which will be scheduled to run at regular intervals. Once we have logged in to the Azure Portal, search for Function App and select it.

Specify the App name and the other Function App details that will be used to create the Azure Function App.

Click on Create to provision the Function App.

Once the Azure Function App is running, we can add the function where we will add the code. Select the Plus option and select ‘Timer Trigger C#’ option to simulate a timer that will run on a schedule.

We can specify a name for the Function and specify the schedule. By default it is set to run every 5 minutes.

Develop the Timer Code
Once created, it will open up the run.csx file with the below default code block.

Before adding the code to access SharePoint Online List, we must upload the SharePoint Client libraries that we will be using within the code. To do this, select the Azure Function App and from Platform Features click on Advanced Tools(Kudu).

From the Debug Console, Select the option ‘PowerShell’.

It will open the folder structure. We must navigate to the site –> wwwroot –> <Function Name> location

The function name is ‘ExpiredSLAIdenitifier’. Click on the folder to go inside the folder where we will create a new folder.

Create the New Folder and let's name it as bin.

Finally drag and drop the client dlls which we can find at,
C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI
If we are working from a Non-SharePoint Server machine, we can download the client dlls from here .The Client Dlls that we will be using are :
- Microsoft.SharePoint.Client.dll
- Microsoft.SharePoint.Client.runtime.dll

Now we can go ahead to the function and add the code that implements the logic.

By default the code fragment will look like below,

Replace the above code with the below section so that it will connect to the SharePoint Online list and fetch the list items whose SLA is today. It will also send a mail to the specified service engineer alerting the action items that has to be resolved.
Code









Sudhanshu KaushikPosted Feb 19, 2023, 8:32 PM
Hi, I am getting this error on line "SPClientContext.ExecuteQuery();". Any ideas? 2023-02-19T20:25:41Z [Information] Azure Function Exception: The remote server returned an error: (400) Bad Request.2023-02-19T20:25:41Z [Information] at System.Net.HttpWebRequest.GetResponse() at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute() at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate() at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest() at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery() at Submission#0.Run(TimerInfo myTimer, ILogger log) in C:\home\site\wwwroot\TimerTrigger1\run.csx:line 36
Eric HalseyPosted Dec 14, 2017, 10:38 PM
Great article! Anyone else getting the following exception? "Azure Function Exception: The Login server cannot issue the requested compact encrypted ticket because a Data Encryption Key (DEK) has not been uploaded to the site."
Mahesh BabuPosted Jul 26, 2017, 5:55 AM
I tried this...it got worked. Thank you.
Mahesh BabuPosted Jul 19, 2017, 4:03 AM
Good Information Priyaranjan... thank you very much.