SharePoint 2013: Use event receivers to handle events in apps for SharePoint

Learn how to handle events in an app for SharePoint by using the SharePoint development tools in Visual Studio 2012.

C# (3.8 MB)
5 Star
7,725 times
Add to favorites
E-mail Twitter Digg Facebook

Solution explorer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.EventReceivers;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace BasicDataOperationsWeb
    public class RemoteEventReceiver1 : IRemoteEventService
        public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
            // When a "before" event occurs (such as ItemAdding), call the event receiver code.
            return new SPRemoteEventResult();

        public void ProcessOneWayEvent(SPRemoteEventProperties properties)
            // When an "after" event occurs (such as ItemAdded), call the event receiver code.            

        public static void ListRemoteEventReceiver(SPRemoteEventProperties properties)
            // This code works only if OAuth on the server that's running SharePoint is set up with an ACS token.
            // Code that works with S2S setup is similar, but some changes are required.
            string logListTitle = "EventLog";

            // Return if the event is from the EventLog list. Otherwise, it may go into an infinite loop.
            if (string.Equals(properties.ItemEventProperties.ListTitle, logListTitle, StringComparison.OrdinalIgnoreCase))

            // Get the token from the request header.
            HttpRequestMessageProperty requestProperty = (HttpRequestMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name];
            string contextTokenString = requestProperty.Headers["X-SP-ContextToken"];

            // If there is a valid token, continue.
            if (contextTokenString != null)
                SharePointContextToken contextToken =
                    TokenHelper.ReadAndValidateContextToken(contextTokenString, requestProperty.Headers[HttpRequestHeader.Host]);

                Uri sharepointUrl = new Uri(properties.ItemEventProperties.WebUrl);
                string accessToken = TokenHelper.GetAccessToken(contextToken, sharepointUrl.Authority).AccessToken;
                bool exists = false;

                // Retrieve the log list "EventLog" and add the name of the event that occurred to it with a date/time stamp.
                using (ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken(sharepointUrl.ToString(), accessToken))
                    List logList = clientContext.Web.Lists.GetByTitle(logListTitle);

                        exists = true;

                    catch (Microsoft.SharePoint.Client.ServerUnauthorizedAccessException)
                        // If the user doesn't have permissions to access the server that's running SharePoint, return.

                    catch (Microsoft.SharePoint.Client.ServerException)
                        // If an error occurs on the server that's running SharePoint, return.
                        exists = false;

                    // Create a log list called "EventLog" if it doesn't already exist.
                    if (!exists)
                        ListCreationInformation listInfo = new ListCreationInformation();
                        listInfo.Title = logListTitle;
                        // Create a generic custom list.
                        listInfo.TemplateType = 100;

                    // Add the event entry to the EventLog list.
                    string itemTitle = "Event: " + properties.EventType.ToString() + " occurred on: " + DateTime.Now.ToString(" yyyy/MM/dd/HH:mm:ss:fffffff");
                    ListCollection lists = clientContext.Web.Lists;
                    List selectedList = lists.GetByTitle(logListTitle);
                    ListItemCreationInformation listItemCreationInfo = new ListItemCreationInformation();
                    var listItem = selectedList.AddItem(listItemCreationInfo);
                    listItem["Title"] = itemTitle;