Introduction
In this article, you will learn how to create Azure function apps for the SharePoint operations using client side object model PowerShell scripts.
Note: Azure functions is 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 works with the internet of things.
In this sample, let us see how to integrate SharePoint operations on to Azure functions app using CSOM PowerShell. Any complex CSOM powershell code can be written and executed on the cloud.
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.
Prerequisites
- Microsoft Azure account with Azure functions app.
- SharePoint online portal
Create Function App
From the azure portal, create new function app.
Under function app, create new function using HttpTrigger-PowerShell template (If the template is not listed, select all options from language and scenario dropdowns).
Importing SharePoint Assemblies
By default only the system assemblies are present on the Azure functions. To load the external assemblies, we need to import the assemblies into the function and then refer to it in the code.
SharePoint CSOM PowerShell assemblies need to be uploaded. 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 (site -> wwwroot -> FunctionName). Create the bin folder and upload the necessary files.
Note
If the deployment credentials are not set, then set the credentials from the app settings page and then proceed uploading the assemblies.
Upload the required reference files (.dll). For SharePoint operations, Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll files are required. These are the client side reference files required for PowerShell CSOM operations. The files can be downloaded from the Microsoft site here.
Create Function
Go back to the trigger function created. Write the necessary code in run.ps1 file under develop section. Refer the assembly files using Add-Type keywords. And then start writing the necessary code for execution.
Copy and paste necessary CSOM code for testing the sample. In the below sample, the code checks if the list already exists on the site using CSOM PowerShell.
- Add-Type -Path "D:\home\site\wwwroot\spopspowershell\bin\Microsoft.SharePoint.Client.dll"
- Add-Type -Path "D:\home\site\wwwroot\spopspowershell\bin\Microsoft.SharePoint.Client.Runtime.dll"
-
- $siteURL = "https://nakkeerann.sharepoint.com/sites/spfx"
- $userId = "[email protected]"
- $plainText= "***"
- $pwd = ConvertTo-SecureString $plainText -AsPlainText -Force
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- $ctx.credentials = $creds
- try{
- $lists = $ctx.web.Lists
- $list = $lists.GetByTitle("TestList")
- $ctx.load($list)
- $ctx.executeQuery()
- if($list){
- Out-File -Encoding Ascii -FilePath $res -inputObject "List Exists"
- }
- }
- catch{
-
- }
Deploy/ Run
Save and run the file. Test panel shows the output of the execution.
The Azure function app URL can be retrieved by clicking on “Get Function URL” link. The trigger can be made from the browser or through any Ajax REST calls by using function app URL.
Summary
In this article, you have learned about creating Azure functions app for SharePoint sites with CSOM PowerShell code.