Introduction
In this article I am going to explain what the Remote Event Receiver is and how we can create in SharePoint 2013, with this example you can also learn how to create SharePoint APP with Announcement List.
Event Receiver
Event is nothing but an action, on doing some action we need to execute some code to do work, there are 2 types of Event Receivers ‘Before’ and ‘After’ events, it can also called as ‘synchronous’ and ‘Asynchronous’ events. We can create events in various levels like Web, List/Libraries, List/Libraries Item, and Feature level.
We can add actions like Item adding, Item Added, Item updating, Item Updated etc…
Remote Event Receiver
Remote Event Receivers are same as event receivers but normal Event Receivers are hosted in SharePoint items, the Remote Event Receivers hosted in remotely for ex… we can host event receivers in SharePoint Apps.
In this example I am going to create a SharePoint App with an Announcement list, and I am going to create Remote Event Receiver on this Announcement list, added an event on Item adding, when user adding the item on the Announcement list the event is going to fire and update the item title with some text.
As this events are running remotely (Apps), so we are calling as Remote Event Recivers.
Steps to create Remote Event Receiver.
Step: 1
Create SharePoint APP Application called ‘RKSHAppWithList’ and pass the required SP web to deploy this APP.
Step: 2
Add new list item in the APP, select the announcement template.
Step: 3
Create WebPart Zone in the default page and add Announcements list to that. Below code will help you to add WebPart Zone and add announcement list.
Code:
- <h1>My App with Announcement List !!!</h1>
- <br />
- <br />
- <!-- Adding webpart zone to add the announcement list -->
- <WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly"
- ID="full" Title="loc:full">
- <WebPartPages:XsltListViewWebPart ID="XsltListViewWebPart1"
- runat="server" ListUrl="Lists/Announcements" IsIncluded="True"
- NoDefaultStyle="TRUE" Title="Announcements" PageType="PAGE_NORMALVIEW"
- Default="False" ViewContentTypeId="0x">
- </WebPartPages:XsltListViewWebPart>
- </WebPartPages:WebPartZone>
Step: 4
Add New Remote Event Receiver to the App, this will add additional Remote Event Receiver service. The Remote Event Receivers work like a service.
Step: 5
Add Receiver code to change the title on item adding.
Code:
- 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();
- }
- }
- }
Step: 6
Build the Remote Event Service application and browse the service to confirm the service is running with no issues.
Step: 7
Copy the Service url and replace that in App application Element file url to link APP with Remote Event Receiver with SharePoint APP.
Step: 8
Change the SharePoint App property (Web Project to ‘None’), this is not only the web project we are also running the remove service in this.
Step: 9
Deploy the SharePoint App in the configured SP web.
Browse the APP and try to add the new Announcement item, once the item is created the Remote Event Receiver will run and change the title to updated one.
Hope this article will help in understanding the SharePoint APP and Remote Event Receiver. Happy Coding.
Read more articles on SharePoint: