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.

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

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

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

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.

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

 

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

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

 

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.

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

 

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

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

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

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

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

 

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

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

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

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)

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

 

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

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

 

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.

 

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

 

 

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.

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

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.

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

 

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

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

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

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

 

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.

 

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

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.

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

 

Execute the timer job

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

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

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.

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)

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.

 

SharePoint Online Timer Jobs with Azure functions Using PnP (Part 2)