Skip to content

Remote Event Receivers not getting a valid token when using the "Copy to" action cross site collection #8573

@stevebeauge

Description

@stevebeauge

Target SharePoint environment

SharePoint Online

What SharePoint development model, framework, SDK or API is this about?

SharePoint Add-ins

Developer environment

Windows

What browser(s) / client(s) have you tested

  • 💥 Internet Explorer
  • 💥 Microsoft Edge
  • 💥 Google Chrome
  • 💥 FireFox
  • 💥 Safari
  • mobile (iOS/iPadOS)
  • mobile (Android)
  • not applicable
  • other (enter in the "Additional environment details" area below)

Additional environment details

SP Addin built with .Net 4.8 and VS 2022

Describe the bug / error

Remote Event Receivers allows to intercept item updating or item adding event, and item updated or item added event.

However, these event has empty context token when action is issued from another site collection, especially using the standard OOB "Copy to action" from a document library. These leads to issues when some operation has to be made on the target site collection.

I have two site collections (let call them src and dest)

In the dest site collection I have a remote event receiver that triggers whenever a document is updated in the default document library (ItemAdded, ItemAdding, ItemUpdated and ItemUpdating event are tracked).

This works perfectly when I work whithin the site collection dest (drag and drop file from windows explorer or using the "copy to" action within the same site collection.

However, if I copy a file from src to dest using the OOB copy to action, the RER fails, having in the payload this error:

Failed to get the context token for the remote event receiver url.

After some tests, I land to the conclusion this is due to the browser session creating an auth token for the site src to call the copy file api.

Then the RER triggers using the context token for site src not site dest and fails because of that.

How to overcome this issue ?

Some details:

  • the RER is registered using an application. It handles AppInstalled event to register the receivers onto the host site's document library under the RER app context.

  • the error is taken from the incoming payload, in the SPRemoteEventProperties.ErrorMessage node

  • The RER is built using the addin model (.app package), uploaded in the site collection's app catalog, then activated (from add an app menu)

  • the manifest declare this permissions request :

    <AppPermissionRequests AllowAppOnlyPolicy="true">
      <AppPermissionRequest Scope="http://sharepoint/content/sitecollection"
                            Right="FullControl" />
    </AppPermissionRequests>
    

As I said, it's working perfectly when working within the dest site.

sample SOAP payloads:

Payload of the ItemAdded event, if file copy within the same site collection :

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <ProcessOneWayEvent xmlns="http://schemas.microsoft.com/sharepoint/remoteapp/">
            <properties xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <AppEventProperties i:nil="true"/>
                <ContextToken>eyJ0eXAiOiJKV1QiLC .........</ContextToken>
                <CorrelationId>8d8d6ba0-f0d7-5000-72d5-187dc612d273</CorrelationId>
                <CultureLCID>1033</CultureLCID>
                <EntityInstanceEventProperties i:nil="true" />
                <ErrorCode />
                <ErrorMessage />
                <EventType>ItemAdded</EventType>
                <ItemEventProperties>
                    <AfterProperties xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
                        <a:KeyValueOfstringanyType>
                            <a:Key>somefield</a:Key>
                            <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">14</a:Value>
                        </a:KeyValueOfstringanyType>
                        ... othter fields omitted for brievety ...
                    </AfterProperties>
                    <AfterUrl>Documents partages/myfile.pdf</AfterUrl>
                    <BeforeProperties xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
                    <BeforeUrl i:nil="true"/>
                    <CurrentUserId>14</CurrentUserId>
                    <ExternalNotificationMessage i:nil="true"/>
                    <IsBackgroundSave>false</IsBackgroundSave>
                    <ListId>96e3b5dc-7e94-4db9-b2cd-f0bbda1a59ab</ListId>
                    <ListItemId>64</ListItemId>
                    <ListTitle>Documents</ListTitle>
                    <UserDisplayName>Steve BEAUGÉ</UserDisplayName>
                    <UserLoginName>i:0#.f|membership|steve@mytenant.onmicrosoft.com</UserLoginName>
                    <Versionless>false</Versionless>
                    <WebUrl>https://mytenant.sharepoint.com/sites/mycoll</WebUrl>
                </ItemEventProperties>
                <ListEventProperties i:nil="true"/>
                <SecurityEventProperties i:nil="true"/>
                <UICultureLCID>1033</UICultureLCID>
                <WebEventProperties i:nil="true"/>
            </properties>
        </ProcessOneWayEvent>
    </s:Body>
</s:Envelope>

Payload when copied from different site collections :

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <ProcessOneWayEvent xmlns="http://schemas.microsoft.com/sharepoint/remoteapp/">
            <properties xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <AppEventProperties i:nil="true"/>
                <ContextToken/>
                <CorrelationId>cbf86ba0-0089-5000-5f44-207718e56ee9</CorrelationId>
                <CultureLCID>1033</CultureLCID>
                <EntityInstanceEventProperties i:nil="true"/>
                <ErrorCode>GetContextTokenError</ErrorCode>
                <ErrorMessage>Failed to get the context token for the remote event receiver url https://url/of/my/function.</ErrorMessage>
                <EventType>ItemAdded</EventType>
                <ItemEventProperties>
                    <AfterProperties xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
                        <a:KeyValueOfstringanyType>
                            <a:Key>somefield</a:Key>
                            <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">14</a:Value>
                        </a:KeyValueOfstringanyType>
                        ... othter fields omitted for brievety ...
                    </AfterProperties>
                    <AfterUrl>Documents partages/myfile.pdf</AfterUrl>
                    <BeforeProperties xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
                    <BeforeUrl i:nil="true"/>
                    <CurrentUserId>1073741823</CurrentUserId>
                    <ExternalNotificationMessage i:nil="true"/>
                    <IsBackgroundSave>false</IsBackgroundSave>
                    <ListId>96e3b5dc-7e94-4db9-b2cd-f0bbda1a59ab</ListId>
                    <ListItemId>64</ListItemId>
                    <ListTitle>Documents</ListTitle>
                    <UserDisplayName>Compte système</UserDisplayName>
                    <UserLoginName>SHAREPOINT\system</UserLoginName>
                    <Versionless>false</Versionless>
                    <WebUrl>https://mytenant.sharepoint.com/sites/mycoll</WebUrl>
                </ItemEventProperties>
                <ListEventProperties i:nil="true"/>
                <SecurityEventProperties i:nil="true"/>
                <UICultureLCID>1033</UICultureLCID>
                <WebEventProperties i:nil="true"/>
            </properties>
        </ProcessOneWayEvent>
    </s:Body>
</s:Envelope>

Differences :

  • when same site, my actual user account is set in the UserDisplayName and UserLoginName. When different sites, its system account
  • When same site, ContextToken is provider, no error message. When different sites, ContextToken is empty, but ErrorMessage contains the error

Steps to reproduce

  1. Implement a RER project that subscrive to some event on host web
  2. Copy file within the same site collection using Copy file action in the command bar ==> working
  3. Copy file from another site collection into the library where the RER is set up ==> not working.

A full repro sample is available here : https://github.com/stevebeauge/Repro.MissingContext.

Press F5 from the solution to deploy and add the application to a site, a sample MyList library will be created with RER plugged.

Expected behavior

Remote Event Receivers SOAP payloads should contains a valid context token when triggerd by a user.

Whether the action is executed from the site collection or not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:csom/rest/apiCategory: SharePoint Client Side Object Model SDK / REST APItype:archive-old-issueIssues which are closed as tool old for active worktype:bug-suspectedSuspected bug (not working as designed/expected). See “type:bug-confirmed” for confirmed bugs.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions