SharePoint Remote Timer Jobs With Azure Functions Using CSOM

Introduction

In this article, you will see how to create SharePoint remote timer jobs with client side code, using Azure functions. Client Side Object Model will be used to create the timer jobs on Azure functions. 

The necessary client side code can be directly used on Azure function file. The scheduling will also be done on Azure functions only. Prior to it, this was handled, using Azure jobs and Windows scheduler. 

Create Function app

Create Function app, if it’s not created already. The screenshot is given below of the app creation.

 

Once created, navigate to the app and create a Timer function. Here, choose TimerTrigger-CSharp option. Provide the name for the function. Specify the schedule details. Schedule has to be provided in the cron expression format. The format is given below to specify the schedule. 

  1. {second} {minute} {hour} {day} {month} {day-of-week}  

For example, the cron expression to run Azure function everyday at 4pm can be, as shown below.

  1. 0 0 16 * * *  

The detailed documentation can be found on Microsoft documentation site at https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer 

 

Necessary references need to be uploaded to Azure functions platform (bin folder). In this example, you will see hardcoding the default (admin) user credentials on the written code. 

Actual Logic/ Code

Once the function is created, navigate to the function main function app. Select platform features and advanced tool (Kudu).

 

In Kudu Services app, from the debug control dropdown, select PowerShell option. Diagnostic center will open. Site -> wwwroot -> <yourfunction folder> -> Create bin folder. Upload SharePoint reference DLLs. 

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll

Go to the created function (TimerTriggerCSharp1). By default, run.csx opens.

In this sample, let us look at retrieving the total items created with one empty column every day.

The piece of snippet given below loads the necessary assemblies. It also contains the code to retrieve the total number of items created with an empty column in the list TestList. Copy the code given below and paste it on run.csx file.

  1. #r "Microsoft.SharePoint.Client.Runtime.dll"  
  2. #r "Microsoft.SharePoint.Client.dll"  
  3. using System;  
  4. using System.Net;  
  5. using Microsoft.SharePoint.Client;  
  6.   
  7.   
  8. public static void Run(TimerInfo myTimer, TraceWriter log)  
  9. {  
  10.     log.Info($"C# Timer trigger function executed at: {DateTime.Now}");      
  11.   
  12.     // parse query parameter  
  13.     string name = "TestList";  
  14.           
  15.     if(name != null){  
  16.         // Input Parameters  
  17.         string siteUrl = "https://nakkeerann.sharepoint.com/";  
  18.         string userName = "[email protected]";  
  19.         string password = "****";  
  20.         System.Security.SecureString securestring = new System.Security.SecureString();  
  21.         foreach (char ch in password)  
  22.             securestring.AppendChar(ch);  
  23.         SharePointOnlineCredentials creds = new SharePointOnlineCredentials(userName, securestring);  
  24.         try{  
  25.             // Get and set the client context  
  26.             // Connects to SharePoint online site using inputs provided  
  27.             using (var clientContext = new ClientContext(siteUrl))  
  28.             {  
  29.                 clientContext.Credentials = creds;  
  30.                 Web web = clientContext.Site.RootWeb;  
  31.                 List list = web.Lists.GetByTitle("TestList");  
  32.                 CamlQuery camlQuery = new CamlQuery();    
  33.                 camlQuery.ViewXml = "<View><Query><Where><IsNull><FieldRef Name='Column1'/>" +    
  34.                 "</IsNull></Where></Query><RowLimit>100</RowLimit></View>";    
  35.                 ListItemCollection items = list.GetItems(camlQuery);  
  36.   
  37.                 clientContext.Load(items);   
  38.                 clientContext.ExecuteQuery();   
  39.   
  40.                 log.Info($"Empty items: {items.Count}");  
  41.             }  
  42.         }  
  43.         catch(Exception ex){  
  44.             log.Info($"Exception: {ex.Message}");  
  45.         }  
  46.     }  
  47.     else{  
  48.         log.Info(name);  
  49.     }  
  50.   
  51. }  
Deploy/ Run

Test the code by saving and running it. Logs file shows the trace and any logging.

In the integrate Windows, you can change the running schedules, whenever required.

Monitor Window shows the log files with success/failure status.

 

Summary

Thus, you have learned running the remote timer jobs for SharePoint Online, using Azure Functions app.