Creating Simple Event Receiver in SharePoint 2013

In this article I would like to explain how to create an event receiver for a document library and custom list level in SharePoint 2013.

Here I have performed the following operations.

  1. Document Adding
  2. Document Updating
  3. Document Deleting

Step 1

Open Visual Studio 2013 and create an Empty SharePoint Project.

create Empty SharePoint Project

Step 2

Enter site URL and select Deploy as a farm solution and click Finish.

Enter site URL

Step 3

Open Solution Explorer and add a new item.

add new item

Step 4

Select event receiver and enter the title of the event receiver. Click the Add button.

event receiver

Step 5

Select List Item Events in type of event receiver and select the Document Library option from the event source. Then select an events as in the following and click Finish.

select an events

Step 6

The following screen will appear. Here I'm trying to update a SharePoint list based on file changes happening to a separate SharePoint Document Library.

Basically, the list will act like a document log. So we need to create the library and a list.

Here, I have created the EmployeeDocuments library to add and maintain documents and also created an EmployeeDocumentLog list to log the changes happening to the library.

changes happening to the library

An EmployeeDocuments Document Library has the following columns:

  1. Name
  2. Modified
  3. ModifiedBy

An EmployeeDocumentLog List has the following columns:

  1. Title
  2. DocumentAction

Step 7

Go to the Event receiver cs file and paste the following code.

Add Document

Here note that the ItemAdded method is used instead of the ItemAdding method.

  1. public override void ItemAdded(SPItemEventProperties properties) {  
  2.     //base.ItemAdding(properties);  
  3.     using(SPWeb web = properties.OpenWeb()) {  
  4.         try {  
  5.             SPList list = web.Lists["EmployeeDocumentLog"];  
  6.             SPListItem newItem = list.Items.Add();  
  7.             newItem["Title"] = properties.ListItem.Name;  
  8.             newItem["DocumentAction"] = "Document Added";  
  9.             newItem.Update();  
  10.         } catch (Exception ex) {  
  11.             throw ex;  
  12.         }  
  13.     }  
  14. }  
Update Document
  1. public override void ItemUpdating(SPItemEventProperties properties) {  
  2.     //base.ItemUpdating(properties);  
  3.     using(SPWeb web = properties.OpenWeb()) {  
  4.         try {  
  5.             SPList list = web.Lists["EmployeeDocumentLog"];  
  6.             SPListItem newItem = list.Items.Add();  
  7.             newItem["Title"] = properties.ListItem.Name;  
  8.             newItem["DocumentAction"] = "Document Updated";  
  9.             newItem.Update();  
  10.         } catch (Exception ex) {  
  11.             throw ex;  
  12.         }  
  13.     }  
  14. }  
Delete Document:
  1. public override void ItemDeleting(SPItemEventProperties properties)  
  2. {  
  3.     //base.ItemDeleting(properties);  
  4.     using (SPWeb web = properties.OpenWeb())  
  5.     {  
  6.         try  
  7.         {  
  8.             SPList list = web.Lists["EmployeeDocumentLog"];  
  9.             SPListItem newItem = list.Items.Add();  
  10.             newItem["Title"] = properties.ListItem.Name;  
  11.             newItem["DocumentAction"] = "Document Deleted";  
  12.             newItem.Update();  
  13.         }  
  14.         catch (Exception ex)  
  15.         {  
  16.             throw ex;  
  17.         }  
  18.     }  
  19. }  
Step 8

Here targeting to add the event receiver only to the “EmployeeDocuments” library, the Elements.xml file requires the following change.

Elements
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
  3.   <!--<Receivers ListTemplateId="101">-->  
  4.   <Receivers ListUrl="EmployeeDocuments">  
  5.       <Receiver>  
  6.         <Name>EmployeeDocumentsLogItemAdding</Name>  
  7.         <Type>ItemAdded</Type>  
  8.         <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>  
  9.         <Class>DocumentEventReceiver.EmployeeDocumentsLog.EmployeeDocumentsLog</Class>  
  10.         <SequenceNumber>10000</SequenceNumber>  
  11.       </Receiver>  
  12.       <Receiver>  
  13.         <Name>EmployeeDocumentsLogItemUpdating</Name>  
  14.         <Type>ItemUpdating</Type>  
  15.         <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>  
  16.         <Class>DocumentEventReceiver.EmployeeDocumentsLog.EmployeeDocumentsLog</Class>  
  17.         <SequenceNumber>10000</SequenceNumber>  
  18.       </Receiver>  
  19.       <Receiver>  
  20.         <Name>EmployeeDocumentsLogItemDeleting</Name>  
  21.         <Type>ItemDeleting</Type>  
  22.         <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>  
  23.         <Class>DocumentEventReceiver.EmployeeDocumentsLog.EmployeeDocumentsLog</Class>  
  24.         <SequenceNumber>10000</SequenceNumber>  
  25.       </Receiver>  
  26.   </Receivers>  
  27. </Elements>  
Step 9

Build and deploy the solution.

deploy

Step 10

The document has been added.

Go to the EmployeeDocuments Document Library and add a new document.

add new document

document library

Go to “EmployeeDocumentLog” list. The log will show by the following.

log

Step 11

The document has been updated.

Go to the EmployeeDocuments library and update the document.

EmployeeDocuments library

Here I have changed the File Name.

changed the File Name

Go to the “EmployeeDocumentLog” list. The log will show as in the following:

EmployeeDocumentLog list

Step 12

The document has been deleted.

Go to EmployeeDocuments library and delete the document.

EmployeeDocuments

Go to the “EmployeeDocumentLog” list. The deleted document log will show as in the following:

EmployeeDocumentLog

Summary

In this article we saw have how to create an event receiver for a Document Library and custom list level in SharePoint 2013.