Forum Discussion
Copy file action and Remote Event Receivers not getting a valid token
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`.
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 code is written using an Azure Function (following s-kainet blog article)
- 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.
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|email address removed for privacy reasons</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