SharePoint Remote Event Receivers using Azure Function

Overview 

SharePoint Remote Event Receiver is a feature available in SharePoint Online which allows to perform custom actions when an event has been occurred for SharePoint list item. These event receivers can be invoked on synchronous and asynchronous list events. Suppose I have two list named “List A“ and “List B”. Whenever an item is updated in “List A”, a new item should be added in “List B”. For this, a remote event receiver can be attached to “List A”, which can push data in “List B” when item is updated in “List A”.

There are flows available in Power Platform which can be used to perform custom actions whenever any event occurs in list. But there are several limitations to it:
*

  • Few list operation triggers are not available in Microsoft Flow such as Move operations.
  • If same set of actions needs to be performed for multiple sites/ list, a separate flow needs to be created for each list/ site. However, in this approach one azure function can be registered as an event receivers for multiple sites.

To read more about Remote Event Receivers, please refer to below article: Use remote event receivers in SharePoint | Microsoft Docs

In this article, we will create "ListUpdated" Event Receiver for SharePoint List using PnP PowerShell and Azure Function.

Scenario

Let’s consider a simple scenario for implementation of Event Receivers:

We have 20 SharePoint sites having “Site Pages” library. A certain set of custom actions needs to be performed whenever site pages is updated in any of these sites.

Approach

To achieve the above requirement:

  1. We will create an Http triggered Azure function 
  2. We will use PnP PowerShell command to register above Azure Function as Remove Event Receiver for all the “Site Pages” library in 20 sites.

Implementation

**Step 1 : Create Http Triggered Azure Function **

  1. Create a Function App in Azure Portal. Provide the Function App Name and Resource Group. Select the Runtime Stack as “PowerShell” as we will be using PowerShell script to write our custom logic.
  2. Add a new HTTP Triggered function in Function App created in previous step
  3. Copy the Azure function URL to register it as a Remote Event Receiver in SharePoint List

Step 2 : Register SharePoint List Event Receiver
**
**

Now, we will register SharePoint list for each of the site using the PnP PowerShell command. The event receiver will be called on “ItemUpdated” event.

$username= "Username@tenant.onmicrosoft.com"
$password= "Password"
$siteUrl= “Provide your siteurl” 
 
$encpassword = convertto-securestring -String $password -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $encpassword   
 
Connect-PnPOnline -Url $siteUrl -Credentials $cred -ErrorAction Stop
$functionUrl= “Provide your Function URL copied in previous step”
 
Add-PnPEventReceiver -List "Site Pages" -Name HandbookEventReceiver -Url $functionUrl -EventReceiverType ItemUpdated -Synchronization Asynchronous

Step 3 : Write your custom logic in Azure Function

using namespace System.Net
 
  
 
# Input bindings are passed in via param block.
 
param($Request, $TriggerMetadata)
 
  
 
# Write to the Azure Functions log stream.
 
Write-Host "PowerShell HTTP trigger function processed a request."
 
  
#fetching the event receiver response for the event triggered
$xmlDocument=[xml]$Request.Body
 
#fetch the ID of the item which ahs been updated
 
 $itemId= $xmlDocument.Envelope.Body.ProcessOneWayEvent.properties.ItemEventProperties.ListItemId
 
#fetch the site URL on which event has been happened
 
 $siteUrl=$xmlDocument.Envelope.Body.ProcessOneWayEvent.properties.ItemEventProperties.WebUrl
 
 #fetch the after properties for vti_level
 
 $vtiLevelAfter= Foreach ($Key in ($xmlDocument.Envelope.Body.ProcessOneWayEvent.properties.ItemEventProperties.AfterProperties.ChildNodes.GetEnumerator() | Where-Object {$_.Key -eq "vti_level"})){$Key.Value.'#text'}
 
 #fetch the before properties for vti_level
 
 $vtiLevelBefore= Foreach ($Key in ($xmlDocument.Envelope.Body.ProcessOneWayEvent.properties.ItemEventProperties.BeforeProperties.ChildNodes.GetEnumerator() | Where-Object {$_.Key -eq "vti_level"})){$Key.Value.'#text'}
 
  
#check if the major version of the page has been checked-in
if($vtiLevelAfter -eq 1)
 
{
    # Write custom logic to perform required actions when an item is updated.
}

Points to note in above code:

  1. Whenever an update is made in “Site Pages” library on the site where we have registered the Event receiver, the above function will be called.
  2. The Request.body will contain all the information about the update made in the SharePoint list.
  3. We can fetch the information about the site, list, and item on which the operation has been made by the user.
  4. AfterPropoerties and BeforeProperties provide the information about the list item change i.e., properties values before the update and properties values after the update.
  5. “Vti_level” provides the information about the page publish state.

Event

Before Properties[RJ1] 

After Properties

Check-in Minor Version

255

255

Check-in Major Version

255

1

Publish major version

2

2

In above code, the custom code will be executed whenever a major version of the page is checked during the page update event.

Conclusion

Using the above explained approach, a single Azure function can be registered as Event Receiver for multiple sites where same actions needs to be performed on list events. Azure function also replaces the approach to create Remote Event receives as Provider Hosted App.