I am working on migrating our sharepoint on-premises to Office 365. now i manage to migrate the list and libraries and site settings using third party tool.
Next step is that i need to re-implement our Event Receivers to work as Remote ER. so i tried to create a very simple test RER,as my first RER experience, but i fail to do so.
I tried using Provider-hosted add-in and then using SP-hosted add-in without any success, as follow:-
Using Provider-Hosted Approach
-
I created a developer site collection.
-
Then i open visual studio 2015 professional, i create a new sharepoint add-in
-
I chose type as provider-hosted add-in.
-
And type as SharePoint-online:-
-
type using MVC.
-
Authentication as AZURE.
-
Then i right click on the solution>>i add a new remote event receiver.
-
Then i define that the event is item adding on custom lists.
-
Then i added the following code inside my RER, to update the title of the item to be equal to “updated title”, as follow:-
public class RemoteEventReceiver1 : IRemoteEventService { /// <summary> /// Handles events that occur before an action occurs, such as when a user adds or deletes a list item. /// </summary> /// <param name="properties">Holds information about the remote event.</param> /// <returns>Holds information returned from the remote event.</returns> public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties) { SPRemoteEventResult result = new SPRemoteEventResult(); using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties)) { if (clientContext != null) { if (properties.EventType == SPRemoteEventType.ItemAdding) { result.ChangedItemProperties.Add("Title", "updated title"); } // clientContext.Load(clientContext.Web); // clientContext.ExecuteQuery(); } } return result; } /// <summary> /// Handles events that occur after an action occurs, such as after a user adds an item to a list or deletes an item from a list. /// </summary> /// <param name="properties">Holds information about the remote event.</param> public void ProcessOneWayEvent(SPRemoteEventProperties properties) { using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties)) { if (clientContext != null) { clientContext.Load(clientContext.Web); clientContext.ExecuteQuery(); } } } }
-
Then i build and run my application, where i got a page to trust the app.
-
After trusting it, i was redirected to an asp.net MVC web application. Where the MVC url will be as follow:-
https://localhost:44356/?SPHostUrl=https//*****
Using SP-Hosted App
-
I create a new SharePoint add-in
-
I select it as SharePoint-Hosted app.
-
version as SharePoint-online.
-
after that i right click on the project and i add a new item of type RER. i specify to run when list item are being added on custom lists(similar as above).
-
inside the RER i entered this code (similar to the above)
SPRemoteEventResult result = new SPRemoteEventResult(); using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties)) { if (clientContext != null) { if (properties.EventType == SPRemoteEventType.ItemAdding) { result.ChangedItemProperties.Add("Title", "updated title"); } // clientContext.Load(clientContext.Web); // clientContext.ExecuteQuery(); } } return result;
- Now i select the
AppManifist.xml
file >> right click on it >> view code >> i define the security as Internal
<AppPrincipal> <Internal /> </AppPrincipal>
- then inside the
web.config
i commented out the client ids:-
<appSettings> <!--<add key="ClientId" value="" /> <add key="ClientSecret" value="kB1iTFbzhSfByM5aeligELKoM8uaTtKl02pNmIYd1BM=" />--> </appSettings>
- then i build and run the project.
Now inside my DEV site collection i already have a custom list, where i added a new item but its title did not change, i tried this when i deploy the Provider-Hosted add-in and then when i deploy the SharePoint-Hosted add-in .
so seems my RER did not fire… can anyone advice what could be causing my provider hosted add-in OR my sharepoint-hosted add-in which contain the RER to not get fired when items are being added?? although i followed the same steps as the one used on many samples and tutorials.