Introduction
In this article, you will learn how to create Azure function apps for SharePoint operations, using PnP Core CSOM library components.
Azure functions are the ways to run the solutions or pieces of code over the Cloud. It helps in processing the data or integrating other systems or even working with the Internet of Things.
The main advantage of using PnP Core libraries will be the reduced code to get the required information. The required object can be retrieved with just single line of code, once the client context is set. PnP core CSOM documentation can be found on Office site here. PnP Core CSOM library packages can be downloaded here.
The references used for testing the sample are given below.
- Microsoft.SharePoint.Client.
- Microsoft.SharePoint.Client.Runtime.
- OfficeDevPnP.Core.
In this sample, let us see how to integrate SharePoint operations on to Azure functions app, using CSOM PowerShell. Any complex code (CSOM) can be written and executed on Cloud.
The functions can be triggered in multiple ways. Here, we will create HTTP trigger function. More details and pricing information about the Azure functions can be found here.
The articles given below also shows step by step approach in integrating SharePoint operations on Azure functions.
Prerequisites
- Microsoft Azure account with Azure functions app.
- SharePoint online portal
Create Function app
From Azure portal, create new Function app.
Under Function app, create new function, using HttpTrigger-CSharp template (If the template is not listed, select all the options from the language and scenario dropdowns).
Importing SharePoint Assemblies
By default, only the system assemblies are present on Azure functions. To load the external assemblies, we need to import the assemblies into the function and then reference it in the code. SharePoint PnP Core CSOM core assemblies need to be uploaded.
The assembly files are extracted by from NuGet site. Follow the site given below to extract the required assemblies.
Upload the required assembly files, using Kudu tool. From the app settings page, select advanced tools under development tools and click on go. The tools page will be opened. Under debug console, select PowerShell. Navigate to the appropriate function under wwwroot folder. Create the bin folder and upload the necessary files. Please refer to my previous articles for step by step approach.
Note- If the deployment credentials are not set, then set the credentials from the app settings page and then proceed uploading the assemblies.
Code
Go back to the created trigger function. Write the necessary code in run.ps1 file under develop section.
Refer the assembly files, using #r and using keywords. Now, start writing the necessary code for the execution.
The code snippet given below checks, if the list already exists on the site and returns the list ID.
- #r "Microsoft.SharePoint.Client.dll"
- #r "Microsoft.SharePoint.Client.Runtime.dll"
- #r "OfficeDevPnP.Core.dll"
- using System.Net;
- using Microsoft.SharePoint.Client;
-
- public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
- {
- string siteUrl = "https://nakkeerann.sharepoint.com/";
- string userName = "[email protected]";
- string password = "***";
- OfficeDevPnP.Core.AuthenticationManager authManager = new OfficeDevPnP.Core.AuthenticationManager();
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
-
- string listName = "TestList";
-
- List list = clientContext.Site.RootWeb.GetListByTitle(listName);
- if (list != null)
- {
-
- return req.CreateResponse(HttpStatusCode.OK, list.Id);
- }
- else
- {
- log.Info("List is not available on the site");
- }
-
- }
- }
- catch (Exception ex)
- {
- log.Info("Error Message: " + ex.Message);
- }
- return req.CreateResponse(HttpStatusCode.OK, "End of function");
-
- }
Deploy/ Run
Save and run the file. The output results can be seen on the test panel of Azure function app portal.
Azure function app URL can be retrieved by clicking Get Function URL link. The trigger can be made from the Browser or through any AJAX REST calls by using function app URL.
The article given below helps you in testing Azure function code from SharePoint pages.
Summary
In this article, you have learned about creating Azure functions app for SharePoint sites with PnP Core CSOM library components.