Thursday, April 4, 2013

Create Remote Event Receivers in Share Point 2013

Remote event handlers new feature added in share point 2013. I have already published a post regarding event handling in share point 2013. In this post Iam showing how to create a remote event receiver through Visual studio.

here iam considering a requirement that update title of Announcement list, when ever new item added to the Announcement list. So that creating an app that contains Announcement list and adding remote event handler to the app. The process shown as below,

Create New Project-->Select SharePoint App in Visual Studio 2013.
























App should be SharePoint-hosted




Check the AppManifest.xml file and tag <AppPrincipal> should be <Internal/> as shown the code.

<AppPrincipal>
  <Internal/>
</AppPrincipal>

Add New item as list type of announcements , select list Item Type Events , select ItemAdded event as shown below 







Add new item as Remote Event Receiver to the solution.



After adding the remote event receiver, you'll get AppWeb project as shown in the image below. While creating Remote event receiver was selected list Item events and ItemAdded event. 



Visual studio by default add the Appweb to the project, App web contains the service file that to run the web service. A Remote event receiver web service by default inherits from IRemoteEventService. 

Remote event structure is different than regular event receivers. In regular event receivers we'll over ride the existing methods  Here in the Remote event handlers we'll write the code in two methods, ProceeEvent() and ProcessOneWayEvent(). So the both the methods will handle the Synchronous and Asynchronous events rather than writing every method. Here we can simply called the method by checking SPRemoteEventProperties.EventType enumerator.

ProcessEvent() will return SPRemoteEventResult, shows the status and error message like other server side events. In this method for item event contains dictionaries for before and after properties, list, user information.

Here i took Item Added event. So that i need to add the code in ProcessOneWayEvent. I was added the following code in the method. After item added to the Announcement list title will be updated through my coding. 
Here is the code in ProcessOneWayEvent() method.


public void ProcessOneWayEvent(RemoteEventProperties properties)
        {
            Uri myurl = new Uri(properties.ItemEventProperties.WebUrl);
            if (properties.EventType == RemoteEventType.ItemAdded)
            {
                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 Remote Event Receiver";

                        itemContact.Update();

                        clientContext.Load(itemContact);

                        clientContext.ExecuteQuery();
                    }
                }
            }
        }


xml in Elements.xml will be


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="10000">
      <Receiver>
        <Name>MyRemoteEventReceiverItemAdded</Name>
        <Type>ItemAdded</Type>
        <SequenceNumber>10000</SequenceNumber>
        <Url>~remoteAppUrl/MyRemoteEventReceiver.svc</Url>
      </Receiver>
  </Receivers>
</Elements>

Here Url is the WCF service url to run the event receiver.

Add the following code to default.aspx page, in "PlaceHolderMain" contentplaceholder to view the list in the app home page.


<div>
       <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>
    </div>

Finally my default.aspx page will be,




Deploy the app and and you'll be redirected to sharepoint app page that containes Announcement list.

Add new item to the announcement list. Title will be updated with "  changed by Remote Event Receiver", in my next post i'll write about app event handlers.

Provide comments below if you have any issues creating the event receiver.

Share this