Microsoft Office Sharepoint

All my posts are provided "AS IS" with no warranties, and confer no rights.

Can I fix my event receivers in SharepointOnline ?

I was looking for a solution. Found none documented , therefore, here it is.

Consider the following scenario: you have a document library where some of the events you are expecting seem not to be working. You troubleshoot your list and find out that some of the event receivers you are expecting to see, are not present.

First a bit of theory about event receivers:

Event receivers are code parts responsible to responding to Sharepoint Events Out of the box, we have the following Event Receivers objects defined. Taking for example one of the events and the receiver attached, the ItemAdded Event from the list will be handling the asynchronous event that occurs after an item is added. Of course, the event receiver is bound to a specific Event host  (like web, List, etc)

In the specific case of the List Item Event Receiver, you can write your own code that makes use of this event, to perform certain operations on the list item once it is added.

Take a look at one example here : https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ff650021(v%3dpandp.10) 

Now, some of the content types that come with Sharepoint have specific event receivers defined, take for example Document sets, so for instance if you look at the content type schema, you will have:

 <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events">
        <spe:Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events">
         <receiver>
            <name>DocumentSet ItemUpdated</name>
            <synchronization>Synchronous</synchronization>
            <type>10002</type>
            <sequenceNumber>100</sequenceNumber>
            <assembly>Microsoft.Office.DocumentManagement, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</assembly>
            <class>Microsoft.Office.DocumentManagement.DocumentSets.DocumentSetEventReceiver</class>
            <data />
            <filter />
          </receiver>
          <receiver>
            <name>DocumentSet ItemAdded</name>
            <synchronization>Synchronous</synchronization>
            <type>10001</type>
            <sequenceNumber>100</sequenceNumber>
            <assembly>Microsoft.Office.DocumentManagement, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</assembly>
            <class>Microsoft.Office.DocumentManagement.DocumentSets.DocumentSetItemsEventReceiver</class>
            <data />
            <filter />
          </receiver>
        </spe:Receivers>
      </XmlDocument>

Now, the point of this blog is what happens when one item receiver is missing and how to fix it (especially in SharePoint Online)

If the Item Added, Item Adding receivers are missing in the list, if you defined shared fields between your document set and the documents inside it, they will not get updated on the documents you upload them in the document set.

How to check ? ( all below focuses on CSOM which you can use both for Onprem as well as SharePoint Online )

First: you need to connect to Sharepoint via CSOM

Second, get the list from the context and verify the event Receivers :

$listname=Read-Host “List Name?”

$web = $ctx.Web
$ctx.load($web)
$lists = $web.Lists
$ctx.Load($lists)
$list= $lists.GetByTitle($listName)
$eventReceivers= $list.EventReceivers
$ctx.Load($eventReceivers)
$contenttypes= $list.ContentTypes
$ctx.load($contenttypes)
$ctx.ExecuteQuery()

List the EventReceivers for documentset:

$eventReceivers |?{$_.ReceiverClass.Contains(“DocumentSet”)}|Select ReceiverName

you should have ( do not worry about the duplicates, they usually are separated by the sequence number , which determines the order of the processing ):

DocumentSet ItemAdding
DocumentSet ItemUpdating
DocSetItemsEventReceiver ItemUpdating
DocumentSet ItemAdded
DocumentSet ItemUpdated

if you do not have them all, here is how to create it again from the content type schema:

List The content Types:

$contenttypes|Select Name,Id

ex:

Name Id
—- —
Document 0x01010074090E8FFF4F644EB3AC065E3F94F342
Folder 0x012000F8059D5035FA21448F186A9FD10B1013
CustomDocset 0x0120D5200062A5F8B91F542F49A2686D22301BFC2900E80FC9A0A7102043863BF925619EEF4A

Choose the Document Set Content Type (the one that starts with 0x0120D520 because Web ContentType Document Set id is always 0x0120D520 and all the child content types be it at web level or list level will start with this, https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ms452896(v%3Doffice.14))

$DocsetCT = $contenttypes.GetByID(“0x0120D5200062A5F8B91F542F49A2686D22301BFC2900E80FC9A0A7102043863BF925619EEF4A”)

$ctx.Load($DocsetCT)
$ctx.ExecuteQuery()

At this point we have the schema which contains the event receivers :

$DSSchema= [xml]$DocsetCT.SchemaXml
$DSSchema.ContentType.XmlDocuments.XmlDocument[2].receivers.Receiver

Name : DocumentSet ItemUpdated
Synchronization : Synchronous
Type : 10002
SequenceNumber : 100
Url :
Assembly : Microsoft.Office.DocumentManagement, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c
Class : Microsoft.Office.DocumentManagement.DocumentSets.DocumentSetEventReceiver
Data :
Filter :

…..

All we need to do now is to re-add the missing event receiver from the schema (as you would with a custom event receiver):

for example if the first one (0 in our enumeration) is missing:

$eventReceiver= $DSSchema.ContentType.XmlDocuments.XmlDocument[2].receivers.Receiver[0]

 

$erD= new-object Microsoft.SharePoint.Client.EventReceiverDefinitionCreationInformation
$erd.EventType = [Microsoft.SharePoint.Client.EventReceiverType]($eventreceiver.Type)
$erD.ReceiverName = $eventreceiver.Name
$erD.ReceiverAssembly = $eventreceiver.Assembly
$erD.ReceiverClass = $eventreceiver.Class
$erD.Synchronization = [Microsoft.SharePoint.Client.EventReceiverSynchronization]($eventreceiver.Synchronization)
$erd.SequenceNumber =$eventreceiver.SequenceNumber

$list.EventReceivers.add($erD)
$list.Update()
$ctx.ExecuteQuery()


Note 1:

<#Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment.
THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that.
You agree:
(i) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded;
(ii) to include a valid copyright notice on Your software product in which the Sample Code is embedded; and
(iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code.#>

Note 2: usually you should not have to do this, but should you find yourself in need , here you go.

Note 3: be advised that some event receivers have dependencies so removing these event receivers might remove other event receivers.

 

Some more resources on Remote Event Receivers: https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/event-receiver-and-list-event-receiver-sharepoint-add-in