Creating Remote Event Receiver In SharePoint 2013

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.

app

Step: 2

Add new list item in the APP, select the announcement template.

list

list

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:

  1. <h1>My App with Announcement List !!!</h1>  
  2.     <br />  
  3.     <br />  
  4.     <!-- Adding webpart zone to add the announcement list -->  
  5.     <WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly"  
  6.         ID="full" Title="loc:full">  
  7.         <WebPartPages:XsltListViewWebPart ID="XsltListViewWebPart1"  
  8.             runat="server" ListUrl="Lists/Announcements" IsIncluded="True"  
  9.             NoDefaultStyle="TRUE" Title="Announcements" PageType="PAGE_NORMALVIEW"  
  10.             Default="False" ViewContentTypeId="0x">  
  11.         </WebPartPages:XsltListViewWebPart>  
  12.     </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.

service

Step: 5

Add Receiver code to change the title on item adding.

Code:
  1. public void ProcessOneWayEvent(SPRemoteEventProperties properties)  
  2. {  
  3.     Uri myurl = new Uri(properties.ItemEventProperties.WebUrl);  
  4.     using (ClientContext clientContext = new ClientContext(myurl))  
  5.     {  
  6.         if (clientContext != null)  
  7.         {  
  8.             List lstContacts =  
  9.                 clientContext.Web.Lists.GetByTitle(  
  10.                     properties.ItemEventProperties.ListTitle  
  11.                 );  
  12.             clientContext.Load(lstContacts);  
  13.             clientContext.ExecuteQuery();  
  14.   
  15.   
  16.             int cnt = lstContacts.ItemCount;  
  17.   
  18.             ListItem itemContact = lstContacts.GetItemById(cnt);  
  19.   
  20.             clientContext.Load(itemContact);  
  21.             clientContext.ExecuteQuery();  
  22.   
  23.             itemContact["Title"] = itemContact["Title"] + "  changed by RER";  
  24.             itemContact.Update();  
  25.   
  26.             clientContext.Load(itemContact);  
  27.             clientContext.ExecuteQuery();  
  28.         }  
  29.     }  
  30. }  
code

Step: 6

Build the Remote Event Service application and browse the service to confirm the service is running with no issues.

service

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.

xml

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.

service

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.

title

Hope this article will help in understanding the SharePoint APP and Remote Event Receiver. Happy Coding.
 
Read more articles on SharePoint:

Up Next
    Ebook Download
    View all
    Learn
    View all