SharePoint event receiver to Azure storage queue

This blog post covers an easy way to add the request body of a SharePoint event receiver into an Azure storage queue. Custom event receivers can’t be configured to add messages directly into an Azure storage queue. We will use an Azure Logic App to modify the information before it can be added to the queue.

You can instruct SharePoint to send the POST request directly into an Azure Function with HTTP trigger, but this would expose you to many more risks. If you custom code fails for some reason, the function would not have a way to reply the operation.

Before you venture into event receivers, also have a look at SharePoint webhooks. Webhooks offer a more reliable approach and are considered a better option if they meet your requirements. But I am not here to compare event receivers and webhooks, so let’s move on.

Diagram

event receiver

At an high level, our solution will:

  1. Use a Logic App to receive the HTTP request from SharePoint
  2. Wrap the XML received with additional tags to that it is considered a valid queue message
  3. Add the new XML object as a queue message

Logic app

Our Logic App will be super simple and composed by only an HTTP trigger and two actions.
Go the the Azure portal and create an Azure Logic App as per the image below. You may also need to create a Storage account and queue if you don’t have one already.
Please note the extra XML tags added in the Compose action. Due to Azure queue format restrictions, these tags are required in order to add the information as a queue message. If format restrictions were not in place, we could instruct SharePoint to send the information directly into the queue.

logic app

Event receiver

Next we will register a custom event receiver in SharePoint, using the HTTP POST URL of the Logic App. A very easy way to do this is using the Add-PnPEventReceiver command from of PnP PowerShell.
For this example, we will use the ItemAdded event. This will trigger a POST request to the Logic App every time a new item is added to a list.
The command will look similar to this:

Add-PnPEventReceiver -List "MyList" -Name "CustomItemAddedEventReceiver" -Url "https://prod-07.ukwest.logic.azure.com:443/workflows/5a7ab0a648b85a26aed0af01241b45f6/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=gVlHkEGFrfjUHsXfzFTh4Yxcsu9vflKi-rKCc2yydgj" -EventReceiverType ItemAdded -Synchronization Asynchronous

Test it

Now go ahead and add a new item to your list. A new message will be added to the Azure queue almost immediately.

If you open the message, you will see the tags added by the Logic App, followed by the typical XML structure used by event receivers.
(image below is showing only a section of the full message)

Next steps

You can now consume the messages in the queue for your business solution, using for example an Azure Function. Simply set the function to trigger when there are new messages in the queue. If the function finds an unexpected error while processing the request, the message is sent back to the queue. If the message fails a few times, if goes to a “poison” queue and you can decide what else to do at that point.

Leave a Reply

Your email address will not be published. Required fields are marked *