Showing posts with label Event receivers in Sharepoint 2013. Show all posts
Showing posts with label Event receivers in Sharepoint 2013. Show all posts

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.

Tuesday, April 2, 2013

Sharepoint 2013- Handling Event receivers

In SharePoint 2013, to handle events in share point app need to create Remote event receivers or App Event receivers. Yes, In SharePoint 2013 event receivers are modified little bit and two new concepts are introduced.

Remote Event Receivers:

Remote event receivers handles events in an item in the app such as list item, library item or web. In remote event receivers, app responds, when ever item added, updated or deleted in a list/library. When ever creating new remote event receiver you can select the type of event receiver. for Eg. you select ListItemEvent, that fires whenever user add/updates/deletes the list item.
  • Remote event handler, a web application should be exists.
  • Web service will be added to the web application  That web service contains the code to handles the event.

Code file contains two methods ProcessEvent() and ProcessOneWayEvent().


ProcessEvent() handles before action happened  means before item added/updated.deleted. This is 2 way Synchronous event. Here 2- way means, remote calls will handled and provides code to the share point to get context token for call backing to share point.
ProcessOneWayEvent() handles after action happened  means after item added/updated/deleted. This is one way Asynchronous event. One way means will just call the web service without callback to share point.

How Remote Events Works?
  • User makes an action (Add new/ Update/delete)
  • Share point communicates to the ACS and asks the Access token for the current Logged-in user information
  • Share point communicated to the web service and what to do for the event in the back end system.
  • Web service will Communicates to the ACS for it's token to do operation in SharePoint back end. Back end System sends Success/Failure message to web service.
Click here to check all the events support remote handler.

following XML shows the elements.xml file that shows remote event receiver item adding. Here URL means the URL of web service contains code to handle the event. 


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

App Event Receivers:

App event receivers can handles the events for an app in share point like App installing, App uninstalling, App deleting.This will be useful thing, you want to send an notification email to the users when an app got deleted. We can handle few app events through this model. we can't update a list item in HandleAppInstalled Event, because there is no list exists in the app web when first installed the app.  

We can handle following events,
  • Handle App Installed
  • Handle App Uninstalling
  • Handle App Upgraded
To handle the event we need to set the property value to True and False to disable the handler. A web service will be added to the web application, that contains the code file to run the handler. We have ProcessEvent(), ProcessOneWayEvent() methods that contains code. InstalledEventEndPoint tag contains web service code file url, in the AppManifest.xml file. To disable the handler we need to make the property value to False for the event in the properties window. 

Will update two more posts regarding how to create remote event handlers and App event handlers.