Wednesday, 17 February 2016

Creating Event Receiver in SharePoint 2013

In this article we are performing Adding,Updating and Deleting operations using Document Library and Lists.

--By Shiva
Step1:

Open the VisualStudio 2012 and Create an Empty SharePoint Project.
Step2:
Enter Site URL in SharePoint Custom Wizard window and Select the radio button as Deploy as a farm solution

Click on Finish Button.

Step3:
Right Click on the ProjectName from the Solution Explorer and select Add =>New Item

Step 4Select event receiver and enter the title of the Event Receiver. Click the Add button.

Step 5Select 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.

Step 6The 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


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 7Go 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 8Here 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 9Build and deploy the solution.

Step 10The document has been added.

Go to the EmployeeDocuments Document Library and add a new document.
Go to “EmployeeDocumentLog” list. The log will show by the following.
Step 11The document has been updated.

Go to the EmployeeDocuments library and update the document.
Here I have changed the File Name. 
Go to the “EmployeeDocumentLog” list. The log will show as in the following:
Step 12The document has been deleted.

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

i hope it is working fine to all ...
Thanks