SharePoint Online Timer Jobs with Azure functions Using PnP - Part 2
Blog
  • Topics
  • Industries
  • Partners

Explore

Topics

Industries

Partners

SharePoint Online Timer Jobs with Azure functions Using PnP – Part 2

Overview

This post is the second part of the blog about creating SharePoint timer job with CSOM PnP code, using Azure Functions. In this part, you will see how to create the Azure Function and Connect to SharePoint Online with Application Credentials.

The source code of the demo project is available at the following location. https://gitlab.com/public-code-peng/demoazurefunctiontimerjob You can use it to smoke test all your Azure Function settings.

High-level steps:

  1. Create Azure AD Application Registration (Part 1)
  2. Create Azure Resource Group and Function App (Part 2)
  3. Connect to SharePoint Online with Application Credentials (Part 2)

Create Azure Resource Group and Function App

The Resource Group (Demo-TimerJobs) must be created first before deployment happens so that a Function App can be created inside of it.

Create Azure Resource Group

Step 1. Navigate to Resource groups, then click the Add button.

Step 2. Provide the Resource Group value Demo-TimerJobs and select the Subscription with Pay-As-You-Go, then click Create button.

Create Function App

The function App is created inside of the Resource Group.

Step 1. Navigate to the Demo-TimerJobs resource group, click on “Add” button.

Step 2. Search for Function App, then click on the Create button.

Step 3.  Provide the App Name DemoAzureFunctionTimerJob, select the Resource Group (Demo-TimerJobs) in previous steps, select Hosting Plan with Consumption Plan and make sure Runtime Stack is.Net, then click Create button.

Step 4. Search for the “DemoAzureFunctionTimerJob” Azure function in the Function Apps, select SSL under Platform features tab.

Step 5. Select Private Certificates (.pfx), then select the Upload Certificate and upload the Demo.pfx file created in previous steps.

Step 6.     Click on the Upload button. The Demo certificate will show as the following screenshot.

Step 7. Navigate to Function App settings change runtime version to ~1

Connect to SharePoint Online with Application Credentials

After the above steps are done, it is time to create an Azure Function solution in Visual Studio 2017. In this article, we will create a demo application that connects to SharePoint Online and count number of documents in Document library. The purpose of this application is to show you how to connect to SharePoint online with Application credentials.

Step 1. Create an Azure Function solution in Visual Studio 2017. (Make sure you have Azure Functions and Web Jobs Tools selected when installing Visual Studio 2017)

Select Azure Functions v1 (.NET Framework) and Timer trigger.

Step 2. Install SharePointPnPCoreOnline package with NuGet Package Manager.

Step 3. Rename the default Function1.cs file to DemoAzureFunctionTimerJob and update its code as following:

The source code of this demo is available at https://gitlab.com/public-code-peng/demoazurefunctiontimerjob

Please check the details of the ContextProvider.cs file to understand how we get the client context with the Application credentials.

Note: The timer job schedule is configured at the first line of the Azure Function with TimerTrigger. The Azure Function is using CRON expressions for setting up the schedule. In our demo application, it means every 5 minutes, the Azure function will be executed.

The RunOnStartup option is enabled for debugging. There is no need to wait for the timer trigger.

For more information about the CRON expressions, please read the following article from Armin Reiter.

https://codehollow.com/2017/02/azure-functions-time-trigger-cron-cheat-sheet/

Deploy Function App via Visual Studio

In this article, we will publish the DemoAzureFunctionTimerJob with Visual Studio 2017.  If you plan to deploy it with the Azure DevOps, please read article Azure function Continuous Deployment with Azure DevOps. The deployment with Visual Studio 2017 is pretty straight forward, right click on the Azure Function project and then click on Publish button.

Select “Select Existing” and “Run from package file”, then click on Publish button

Find the “DemoAzureFunctionTimerJob”, then click OK button. The Azure function is deployed to Azure now.

Update Application Settings

In the demo code, we used the following environment variables:

App Setting Name

Description
AADClientId The Azure AD Application ID
AADTenantId The Tenant ID
AADCertificateThumbprint The Certificate Thumbprint which retrieved when execute .\Get-SelfSignedCertificateInformation.ps1
WEBSITE_LOAD_CERTIFICATES Same as AADCertificateThumbprint
WebUrl The SharePoint Online Web URL

ListTitle

The Documents library title

These environment variables are stored in Application Settings.

Navigate to the Application settings page, add new settings as following:

Make sure the “WEBSITE_LOAD_CERTIFICATES” is set to the Certificate Thumbprint value which retrieved when executing.\Get-SelfSignedCertificateInformation.ps1.

Execute the timer job

Finally, we are ready to execute the timer job. Navigate to the function and click on the Run button.

The following screenshot shows the log window after executing the timer job. It shows in my SharePoint Online Documents library, there only 1 document in it.

In case you need to debug the Azure function, it is pretty straight forward. All you need to do is open the “Cloud Explorer” (Not the server explorer), find the Azure function and Attach Debugger to it.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up

Categories