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.
- Create Azure AD Application Registration (Part 1)
- Create Azure Resource Group and Function App (Part 2)
- 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
Choosing a Global Software Development Partner to Accelerate Your Digital Strategy
To be successful and outpace the competition, you need a software development partner that excels in exactly the type of digital projects you are now faced with accelerating, and in the most cost effective and optimized way possible.
Get the Guide
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.
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
|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|
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.