top of page
Writer's pictureNakkeeran Natarajan

Azure Functions For SharePoint Operations Using PnP PowerShell


Introduction

In this article, you will learn how to create Azure function apps for SharePoint operations, using PnP PowerShell commands.


Azure functions are the way to run the solutions or piece of code over the Cloud. It helps in processing the data or integrating other systems or even working with the internet of things. 


PnP PowerShell modules

The main advantage of using PnP PowerShell over CSOM PowerShell will be the reduced code to get the required information. PnP PowerShell assemblies can be extracted from the installers. To test PnP PowerShell commands on the local machine, the installers are available on the official site here.


Once installed, the assemblies can be extracted from the local paths (mostly the modules will be present here C:\Program Files (x86)\SharePointPnPPowerShellOnline\Modules\SharePointPnPPowerShellOnline).


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.


Functions can be triggered in multiple ways. Here, we will create HTTP trigger function. More details and costing information about Azure functions can be found here


The assemblies should be uploaded to Azure function app repository, which will be explained later in this article.


To start with, I have created a simple azure function, which retrieves total Web features count from the site.


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-PowerShell template (If the template is not listed, select all the options from the language and scenario dropdowns). 


Importing SharePoint assemblies

Upload the required assembly files, using Kudu tool. From the app settings page, select advanced tools under development tools and click Go. The tools page will be opened. Under debug console, select PowerShell. Navigate to the appropriate function under wwwroot folder. Create the modules folder and upload the necessary files.


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 in the code.


SharePoint PnP PowerShell assemblies/ modules needs to be uploaded. Navigate to the installed PnP PowerShell modules folder (explained in the beginning). Drag and drop all the files present under the folder into function app repository).


Note

If the deployment credentials are not set, 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. If required, refer the assembly files , using Add-Type keywords.


The code snippet given below retrieves the total count of active Web features on the site.

  1. $siteURL = "https://nakkeerann.sharepoint.com/sites/spfx"

  2. $userId = "abc@nakkeerann.onmicrosoft.com"

  3. $plainText= "*****"

  4. $pwd = ConvertTo-SecureString $plainText -AsPlainText -Force    

  5. $creds = New-Object System.Management.Automation.PSCredential($userId,$pwd)  

  6. Connect-PnPOnline -Url 

  7. $siteURL -Credentials $creds  

  8. $webFeatures = Get-PnPFeature -Scope Site  Out-File -Encoding Ascii -FilePath 

  9. $res -inputObject 

  10. $webFeatures.Count  


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 article given below helps you in testing Azure function code from SharePoint pages.


Summary

In this article, you have learnt creating Azure functions app for SharePoint sites with PnP PowerShell commands. As a simple POC, I have retrieved the active Web features count from the site.

0 comments

Comments


bottom of page