SharePoint Remote Event Receiver – Item Deleting – Using Provider hosted App


Scenario : We need to capture the person who delete the item in the document in SharePoint. On item deletion event will fire and add item to another list for further record. Code is attached and screen shorts are below step by step.

We have implemented a Remote Event Receiver on Document Library by Provider hosted app.

Pre-Requisites:(1) Office Developer Tool for VS2013  or VS2015, (2) Office 365 Subscription (3) Azure Subscription to host website over ssl.

  • First Create the SharePoint Add-In.
Select SharePoint Project
Select SharePoint Project

 

Select the SharePoint version

2

Select the SharePoint version

3

Choose the web project which will host the web service. As I am a big fan of MVC, So I select MVC.

 

4

 

SharePoint Will Create Two Project one is SharePoint – Add in (App) which will reside in our App Catalog in our SharePoint.

Another web MVC project will host the web services, as SharePoint Event Remote Event Receivers use web services, Client Secret and Client ID to communicate for any event.

Now we will Add Events on our App Installing and App Uninstalling Events. I.e. Whenever App Install, our App will call web service to run one Process Method.

Right Click on our App Solution and Click for properties.

 

5

 

Once we Make Handle App Installed True, VS will create a AppEventServices.svc in our web project.

6

 

 

In AppEventReceivers.svc file a Process Methods Function we will Attached Event to the Doc Library on Item Deleting.

Below mentioned the Receiver Name, and the List Name on which we want to Add Event i.e. Document Library and the Destination List Name

7

Below mentioned is the Process Event Methods which run all the time.

8-b

 

In the Process Event Methods,

8

 

On App Installed Event i.e. when we add App to Site AppInstalled Event will occur. If Event not Exists than attached Event on ItemDeleting

 

 

 

9 10

If ItemDeleting occurs in the Document Library mentioned in the above than HandleItem event will occur.

11

 

12

 

Inside AddItem using CSOM to Add item to the List.

13

Now Build Solution, We are ready to register the website so that they will communicate with SharePoint using Client ID and Client Secret.

 

Go the App Catalog site and open the appregnew.aspx in the layouts.

 

 

14

 

Take the Client ID, Client Secret, and Title of the projects. App Domain the website name (The Azure site or any public hosting Site which can host this web).

 

Redirect url will be the default page of the site of hosted web.

 

To Host our web, we will use Azure which can communicate to SharePoint during item deleting event over ssl.

 

We will go to Azure web portal add new web.

 

15

 

Give any name. Export the publishing profile

 

16

 

For publishing profile, go the new created site, click on overview, Click on Get Publshing profile and export to your pc. This will help to publish the web from VS.

 

17 18 19

Click Create. This will give the details, save it we will need the Client ID and Client Secret to enter in the app and in the website to Communicate. By doing this SP (SharePoint) will know the handshake token.

 

20

 

Now Bind the app and the website using the Client ID and Client Secret. Open the AppMenifex.xml from app. Right Click on show code.

 

 

21

 

Enter the Client ID which we taken previously. This will alert SharePoint that whoever take this ID is can go next step.

 

22

Open the webconfig of website and enter the client secrect and client id.

 

23

 

Leave everything as it is.

 

Now publish the web to azure with the publishing profile.

 

24 25 26

Now publish app as .app file and upload to app catalog.

It will pop up to confirm the Client ID.

 

27

 

 

28

 

Summary.

 

Make sure that the below List and Document Library should exists on the Destination site.

 

29

 

 

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

namespace VDC_AppWeb.Services
{
public class AppEventReceiver : IRemoteEventService
{

private const string ReceiverName = “ItemDeleting”;
private const string ListName = “VDC Documents”;
private const string DestiNationList = “DestList”;
public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
{

SPRemoteEventResult result = new SPRemoteEventResult();
switch (properties.EventType)
{
case SPRemoteEventType.AppInstalled:
HandleAppInstalled(properties);
break;
case SPRemoteEventType.AppUninstalling:
HandleAppUninstalling(properties);
break;
case SPRemoteEventType.ItemDeleting:
HandleItem(properties);
break;
}
return result;
}

private void HandleAppInstalled(SPRemoteEventProperties properties)
{
using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
{
if (clientContext != null)
{
List myList = clientContext.Web.Lists.GetByTitle(ListName);
clientContext.Load(myList, p => p.EventReceivers);
clientContext.ExecuteQuery();
bool rerExists = false;
foreach (var rer in myList.EventReceivers)
{
if (rer.ReceiverName == ReceiverName)
{
rerExists = true;
System.Diagnostics.Trace.WriteLine(“Found existing ItemDeleted receiver at ” + rer.ReceiverUrl);
}
}
if (!rerExists)
{
EventReceiverDefinitionCreationInformation receiver = new EventReceiverDefinitionCreationInformation();
receiver.EventType = EventReceiverType.ItemDeleting;
OperationContext op = System.ServiceModel.OperationContext.Current;
Message msg = op.RequestContext.RequestMessage;
receiver.ReceiverUrl = msg.Headers.To.ToString();
receiver.ReceiverName = ReceiverName;
receiver.Synchronization = EventReceiverSynchronization.Synchronous;
myList.EventReceivers.Add(receiver);
clientContext.ExecuteQuery();
System.Diagnostics.Trace.WriteLine(“Added ItemDeleted receiver at ” + msg.Headers.To.ToString());
}
}
}
}

private void HandleAppUninstalling(SPRemoteEventProperties properties)
{
using (ClientContext clientContext =
TokenHelper.CreateAppEventClientContext(properties, false))
{
if (clientContext != null)
{
List myList = clientContext.Web.Lists.GetByTitle(ListName);
clientContext.Load(myList, p => p.EventReceivers);
clientContext.ExecuteQuery();

var rer = myList.EventReceivers.Where(
e => e.ReceiverName == ReceiverName).FirstOrDefault();

try
{
System.Diagnostics.Trace.WriteLine(“Removing ItemDeleted receiver at ”
+ rer.ReceiverUrl);

//This will fail when deploying via F5, but works
//when deployed to production
rer.DeleteObject();
clientContext.ExecuteQuery();

}
catch (Exception oops)
{
System.Diagnostics.Trace.WriteLine(oops.Message);
}

}
}
}

private void HandleItem(SPRemoteEventProperties properties)
{
using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
{
if (clientContext != null)
{
try
{
List photos = clientContext.Web.Lists.GetById(properties.ItemEventProperties.ListId);
ListItem item = photos.GetItemById(properties.ItemEventProperties.ListItemId);
// FieldUserValue fvv2 = (FieldUserValue);

clientContext.Load(item);
clientContext.ExecuteQuery();
string Titles = Convert.ToString(item[“FileLeafRef”]);
string Location = Convert.ToString(item[“FileRef”]); ;
string FinalLocation = Location.Replace(Titles, ” “);
string CreatedBys = properties.ItemEventProperties.UserDisplayName;
item.Update();
clientContext.ExecuteQuery();

AddItem(Titles, CreatedBys, FinalLocation, properties);
}
catch (Exception oops)
{
System.Diagnostics.Trace.WriteLine(oops.Message);
}
}
}
}
private void AddItem(string Title, string User, string Location, SPRemoteEventProperties properties)
{

using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
{
if (clientContext != null)
{
List oList = clientContext.Web.Lists.GetByTitle(“VDC Delete Info”);

ListItemCreationInformation listItemCreationInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(listItemCreationInfo);
oListItem[“Title”] = Title;
oListItem[“DeleteBy”] = User;
oListItem[“Location”] = Location;
oListItem.Update();
clientContext.ExecuteQuery();
}
}
}

/// <summary>
/// This method is a required placeholder, but is not used by app events.
/// </summary>
/// <param name=”properties”>Unused.</param>
public void ProcessOneWayEvent(SPRemoteEventProperties properties)
{
throw new NotImplementedException();
}

}
}

  •