Azure Functions For SharePoint Operations Using PnP Core CSOM Library


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.

  • 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.


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.

  1. #r "Microsoft.SharePoint.Client.dll"  
  2. #r "Microsoft.SharePoint.Client.Runtime.dll"  
  3. #r "OfficeDevPnP.Core.dll"  
  4. using System.Net;  
  5. using Microsoft.SharePoint.Client;  
  7. public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)  
  8. {  
  9.     string siteUrl = "";  
  10.     string userName = "";  
  11.     string password = "***";  
  12.     OfficeDevPnP.Core.AuthenticationManager authManager = new OfficeDevPnP.Core.AuthenticationManager();  
  13.     try  
  14.     {  
  15.         // Get and set the client context  
  16.         // Connects to SharePoint online site using inputs provided  
  17.         using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  18.         {  
  20.             // List Name input  
  21.             string listName = "TestList";  
  22.             // Retrieves list object using title  
  23.             List list = clientContext.Site.RootWeb.GetListByTitle(listName);  
  24.             if (list != null)  
  25.             {  
  26.                 // Returns required result  
  27.                 return req.CreateResponse(HttpStatusCode.OK, list.Id);              
  28.             }  
  29.             else  
  30.             {  
  31.                 log.Info("List is not available on the site");  
  32.             }  
  34.         }  
  35.     }  
  36.     catch (Exception ex)  
  37.     {  
  38.         log.Info("Error Message: " + ex.Message);  
  39.     }  
  40.     return req.CreateResponse(HttpStatusCode.OK, "End of function");  
  42. }   

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.


In this article, you have learned about creating Azure functions app for SharePoint sites with PnP Core CSOM library components.