HOW TO: Create a Remote Event Receiver for an Auto-hosted App
This blog post is a contribution from Jaishree Thiyagarajan, an engineer with the SharePoint Developer Support team.
Update: Though this post title says “HOW TO: Create a Remote Event Receiver for a SharePoint Hosted App”, behind the scenes, Visual Studio adds a web project, associates remote event receiver service to the web project and changes the app from SharePoint Hosted App to Autohosted app. We just wanted to call this out to avoid any confusion.
Let’s first create a SharePoint Hosted App.
Choose “SharePoint-Hosted” option for “How do you want to host your app for SharePoint” in VS 2012.
Now right-click the AppManifest.xml and open with XML editor.
Make sure that you have an internal element within the AppPrincipal element (as shown below).
<AppPrincipal> <Internal /> </AppPrincipal> |
Here’s my complete AppManifest.xml.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created:cb85b80c-f585-40ff-8bfc-12ff4d0e34a9-->
<App xmlns="https://schemas.microsoft.com/sharepoint/2012/app/manifest"
Name="SampleSPApp"
ProductID="{ccfc0c1e-ffac-4a20-a454-81e6cb7d4c62}"
Version="1.0.0.0"
SharePointMinVersion="15.0.0.0"
>
<Properties>
<Title>SampleSPApp</Title>
<StartPage>~appWebUrl/Pages/Default.aspx?{StandardTokens}</StartPage>
</Properties>
<AppPrincipal>
<AutoDeployedWebApplication />
</AppPrincipal>
</App>
Let’s first add a list within the app; so that whenever an item it added, the remote event receiver will be invoked.
Right-click the project and add “New Item”.
Choose List and give it a name, e.g., “MySampleList”.
Now, let’s add a remote event receiver. Right-click solution explorer and click “Add” and then “New Item”. Choose “Remote Event Receiver” and give it a name, e.g., “MyReR” and click “Add”.
Choose the event receiver. For e.g., let’s use “Item was Added” (ItemAdded – Asynchronous) event.
Once you click “Finish”, a web application is created with a web service that contains a code file.
!!IMPORTANT!! At this time, this is not really a SharePoint-hosted app. VS creates a web application behind the scenes, which actually makes this an Auto-hosted app. This is because remote event receivers does not work with SharePoint-hosted apps for the following reasons:
1. SharePoint-hosted apps can only use JavaScript CSOM. In other words, no server side code is possible.
2. Remote events in SharePoint require a WCF service to call back when a remote event occurs. Again in other words, remote events requires server side code.
3. The only way to host a WCF service for SharePoint to call back when a remote event occurs is to host the service in a remote web project.
4. Thus you see a remote web project being added to the project you created above. Your app will now be an Auto-hosted app and NOT SharePoint-hosted.
5. Auto-hosted app means that you do not have to worry about hosting the remote web project as SharePoint will deploy and host the remote project for you (in Windows Azure web sites).
So, if you want to handle events, your app needs to be either an Auto-hosted app or a Provider-hosted app. Remote events (app events and remote event receivers) are NOT supported in SharePoint-hosted apps.
Now, coming back to the walk-through, if you see the code file of the service, you’d see two methods.
1. ProcessEvent(): This is a synchronous event that handles events that occur before an action occurs. Such as when a user adds or deletes a list item. Two way event that can handle current events (“-ing”) that can callback to SharePoint.
2. ProcessOneWayEvent(): This is a asynchronous event that 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. This is one-way event that can handle past events (“-ed”).
Add the following code to “ProcessOneWayEvent” as we chose to implement the “Item was added” event. The below code just appends the string “changed by RER” to the title of a list item.
public void ProcessOneWayEvent(SPRemoteEventProperties properties)
{
Uri myurl = new Uri(properties.ItemEventProperties.WebUrl);
using (ClientContext clientContext = new ClientContext(myurl))
{
if (clientContext != null)
{
List lstContacts =
clientContext.Web.Lists.GetByTitle(
properties.ItemEventProperties.ListTitle
);
clientContext.Load(lstContacts);
clientContext.ExecuteQuery();
int cnt = lstContacts.ItemCount;
ListItem itemContact =
lstContacts.GetItemById(
cnt
);
clientContext.Load(itemContact);
clientContext.ExecuteQuery();
itemContact["Title"] = itemContact["Title"] + " changed by RER";
itemContact.Update();
clientContext.Load(itemContact);
clientContext.ExecuteQuery();
}
}
}
Now, copy the URL from the properties window of the web application and paste it in the URL section of the elements.xml file of the event receiver of the SharePoint App.
Here’s the URL I see in my properties window.
Your updated XML should look like the one shown below. NOTE: I’ve added the receiver to just one list instance and not to a list template.
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
<Receivers ListUrl="Lists/MySampleList">
<Receiver>
<Name>MyReRItemAdded</Name>
<Type>ItemAdded</Type>
<SequenceNumber>10000</SequenceNumber>
<Url>https://localhost:36511/MyReR.svc</Url>
</Receiver>
</Receivers>
</Elements>
Now, last but not the least, add the below XsltListViewWebPart to the default.aspx page of the SharePoint App, so that we can add items to the list that has been created. Make sure to update the ListUrl property correctly.
<WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly"
ID="full" Title="loc:full" >
<WebPartPages:XsltListViewWebPart ID="XsltListViewWebPart1"
runat="server" ListUrl="Lists/MySampleList" IsIncluded="True"
NoDefaultStyle="TRUE" Title="MySampleList" PageType="PAGE_NORMALVIEW"
Default="False" ViewContentTypeId="0x">
</WebPartPages:XsltListViewWebPart>
</WebPartPages:WebPartZone>
Now, click F5 and deploy the App. While deploying, you’ll be asked to trust the App. Hit the “Trust It” button.
Try adding items to the list named “MySampleList” and see SharePoint 2013 remote event receivers in action. I’ve added an item “RER is cool”. The remote event receiver changed the title as “RER is cool changed by RER”.
Attaching a sample for you to quickly try SharePoint 2013 remote event receivers Download Zipped SampleSPApp
Here are a few more references on this topic:
How to: Create a remote event receiver
Handling events in apps for SharePoint
SharePoint 2013: Use event receivers to handle events in apps for SharePoint
SharePoint 2013: Create a remote event receiver for external data
If you have not already done so, download and install the Microsoft Office Developer Tools for Visual Studio 2012 tool set for building SharePoint solutions for SharePoint 2010 and SharePoint 2013 using Visual Studio 2012.
Hope this post was helpful!
Comments
Anonymous
January 01, 2003
Hi mate, great article, but instead of using int cnt = lstContacts.ItemCount; it's probably better to use int cnt = properties.ItemEventProperties.ListItemId;Anonymous
January 01, 2003
Thanks for this article. This is exactly what I was looking for.Anonymous
January 01, 2003
Hi mate, great article, but instead of using int cnt = lstContacts.ItemCount; it's probably better to use int cnt = properties.ItemEventProperties.ListItemId;Anonymous
January 01, 2003
Hi,Good article but I have problem with Authentication to SharePoint when i'm executing query over ClientContext. My application uses Claims Authentication Types (WIndows authentication). I obtain Unauthorized 404 exception. How can I authorize to sharepoint?Anonymous
January 01, 2003
Sharepoint DevelopersAnonymous
January 01, 2003
this is not <clear>Anonymous
January 01, 2003
Nice post. Here is one more post Explaining Sharepoint 2013 remote Event handlers sureshpydi.blogspot.in/.../create-remote-event-receivers-in-share.htmlAnonymous
April 05, 2013
Nice one. here is the three articles explaining event handlers in sharepoint 2013 sureshpydi.blogspot.in/.../sharepoint-2013-handling-event-receivers.html sureshpydi.blogspot.in/.../create-remote-event-receivers-in-share.html sureshpydi.blogspot.in/.../sharepoint-2013-creating-app-event.htmlAnonymous
May 24, 2013
In App manifest , When to set AppPrincipal as AutoDeployedWebApplication Or Internal? <AppPrincipal> <AutoDeployedWebApplication/> </AppPrincipal> <AppPrerequisites> <AppPrerequisite Type="AutoProvisioning" ID="RemoteWebHost" /> </AppPrerequisites>Anonymous
May 29, 2013
Internal app principal mostly makes sense for SharePoint-hosted apps. It signifies that either there is no web application that is part of the app, or, if there is one, its server code does not need to communicate with SharePoint. In this walkthrough, which starts as an Internal app principal (SharePoint-hosted app), it gets changed to AutoDeployedWebApplication (Autohosted app) when you add a remote event receiver, because remote event receivers are server code and are expected them to interact with the SharePoint object model.Anonymous
July 25, 2013
Nice Article,thanks for sharing this information.Looking forward for more posts like this. <a href="www.raybiztech.com/Sharepoint-Portal-Solutions.html">Sharepoint Developers</a>Anonymous
August 26, 2013
Thanks, This is a great article! I have a problem, I do every thing you explained step by step and my solution run successfully but the event receiver didn't work????????????????Anonymous
October 01, 2013
Suresh Pydi You blatantly STOLE your examples from this article and then post links to them on this page? You didn't even bother to change ANYTHING. Wow.....Anonymous
October 30, 2013
I am bale to change the title using Remote event receiver. Is it possible to send email to the user using Remote event receiver.Anonymous
October 31, 2013
Hi, i followed the example but my receiver wont fire? Abt the part where you add the xsltListViewWebPart, i copied as it is in default.aspx of sharepoint app. Am i missing something else? How can i debug?Anonymous
November 06, 2013
Hi, I followed exactly the example you provided, but my output is just the string that I entered without 'changed by ReR'. I guess remote event receiver is not firing, please give advise. Do I need to enable the 'handle app installation' under Apps for SharePoint Event in the properties corner?Anonymous
November 21, 2013
Hi, I just followed your example, When i was able to debug ,it was working fine,but I tried to deploy the solution - I am getting the error. Correlation Id: 4e2aa7aa-00be-4a4f-83bb-362a567c0e21 ErrorDetail: The remote hosting service is not configured. ErrorType: Configuration ErrorTypeName: Configuration ExceptionMessage: Microsoft.Office.SecureStoreService.Server.SecureStoreServiceTargetApplicationNotFoundException: Target application not found (application id: RemoteAppManagementInfo).Anonymous
April 18, 2014
Hi,
I have the same problem as the others. Everything goes fine with deployment but the event is not working.
I tried to download your solution and change the references.
I tried on online and onprem enviromennts.
I tried to build the solution from scratch 3 times.
i also tried to build a very simple event reciever which simply cancel the event.
Nothing works, what am i missing? Do i need to configure something else, on the SPSite perhaps?Anonymous
June 02, 2014
Has anyone got it working ??
Event is not firing for me...Anonymous
September 10, 2014
This provided sample does not work. Followed the instructions to a "T" and nothing happens. Same as some other people commenting here. Don't waste your time - this does not work.Anonymous
December 10, 2014
thanks. All articles and books (lots of experts) that i have read has never been able to articulate or explain what's going on with RemoteEventReceivers as you've eloquently explain. Its hard to learn things when you don't actually know how they tie in (as least for me,. i learn by visual and correlation instead of memorizing) Microsoft is done a poor job in explaining and documenting this feature.
I am writing this just to thank you.Anonymous
March 11, 2016
Youi are using http://localhost:36511/MyReR.svc as the location of the Sample App Web. Obviously you wouldn't do this in a production environment. The hosting would have to be somewhere. So assuming this code works, how do you put it in a production environment?