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.
- {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.
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.
- #r "Microsoft.SharePoint.Client.Runtime.dll"
- #r "Microsoft.SharePoint.Client.dll"
- using System;
- using System.Net;
- using Microsoft.SharePoint.Client;
-
-
- public static void Run(TimerInfo myTimer, TraceWriter log)
- {
- log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
-
-
- string name = "TestList";
-
- if(name != null){
-
- string siteUrl = "https://nakkeerann.sharepoint.com/";
- string userName = "[email protected]";
- string password = "****";
- System.Security.SecureString securestring = new System.Security.SecureString();
- foreach (char ch in password)
- securestring.AppendChar(ch);
- SharePointOnlineCredentials creds = new SharePointOnlineCredentials(userName, securestring);
- try{
-
-
- using (var clientContext = new ClientContext(siteUrl))
- {
- clientContext.Credentials = creds;
- Web web = clientContext.Site.RootWeb;
- List list = web.Lists.GetByTitle("TestList");
- CamlQuery camlQuery = new CamlQuery();
- camlQuery.ViewXml = "<View><Query><Where><IsNull><FieldRef Name='Column1'/>" +
- "</IsNull></Where></Query><RowLimit>100</RowLimit></View>";
- ListItemCollection items = list.GetItems(camlQuery);
-
- clientContext.Load(items);
- clientContext.ExecuteQuery();
-
- log.Info($"Empty items: {items.Count}");
- }
- }
- catch(Exception ex){
- log.Info($"Exception: {ex.Message}");
- }
- }
- else{
- log.Info(name);
- }
-
- }
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.