Here I have performed the following operations.
- Document Adding
- Document Updating
- Document Deleting
Step 1
Open Visual Studio 2013 and create an Empty SharePoint Project.
Step 2
Enter site URL and select Deploy as a farm solution and click Finish.
Step 3
Open Solution Explorer and add a new item.
Step 4
Select event receiver and enter the title of the event receiver. Click the Add button.
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.
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.
An EmployeeDocuments Document Library has the following columns:
- Name
- Modified
- ModifiedBy
An EmployeeDocumentLog List has the following columns:
- Title
- 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.
- public override void ItemAdded(SPItemEventProperties properties) {
-
- using(SPWeb web = properties.OpenWeb()) {
- try {
- SPList list = web.Lists["EmployeeDocumentLog"];
- SPListItem newItem = list.Items.Add();
- newItem["Title"] = properties.ListItem.Name;
- newItem["DocumentAction"] = "Document Added";
- newItem.Update();
- } catch (Exception ex) {
- throw ex;
- }
- }
- }
Update Document
- public override void ItemUpdating(SPItemEventProperties properties) {
-
- using(SPWeb web = properties.OpenWeb()) {
- try {
- SPList list = web.Lists["EmployeeDocumentLog"];
- SPListItem newItem = list.Items.Add();
- newItem["Title"] = properties.ListItem.Name;
- newItem["DocumentAction"] = "Document Updated";
- newItem.Update();
- } catch (Exception ex) {
- throw ex;
- }
- }
- }
Delete Document:
- public override void ItemDeleting(SPItemEventProperties properties)
- {
-
- using (SPWeb web = properties.OpenWeb())
- {
- try
- {
- SPList list = web.Lists["EmployeeDocumentLog"];
- SPListItem newItem = list.Items.Add();
- newItem["Title"] = properties.ListItem.Name;
- newItem["DocumentAction"] = "Document Deleted";
- newItem.Update();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
Step 8
Here targeting to add the event receiver only to the “EmployeeDocuments” library, the Elements.xml file requires the following change.
- <?xml version="1.0" encoding="utf-8"?>
- <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
-
- <Receivers ListUrl="EmployeeDocuments">
- <Receiver>
- <Name>EmployeeDocumentsLogItemAdding</Name>
- <Type>ItemAdded</Type>
- <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
- <Class>DocumentEventReceiver.EmployeeDocumentsLog.EmployeeDocumentsLog</Class>
- <SequenceNumber>10000</SequenceNumber>
- </Receiver>
- <Receiver>
- <Name>EmployeeDocumentsLogItemUpdating</Name>
- <Type>ItemUpdating</Type>
- <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
- <Class>DocumentEventReceiver.EmployeeDocumentsLog.EmployeeDocumentsLog</Class>
- <SequenceNumber>10000</SequenceNumber>
- </Receiver>
- <Receiver>
- <Name>EmployeeDocumentsLogItemDeleting</Name>
- <Type>ItemDeleting</Type>
- <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
- <Class>DocumentEventReceiver.EmployeeDocumentsLog.EmployeeDocumentsLog</Class>
- <SequenceNumber>10000</SequenceNumber>
- </Receiver>
- </Receivers>
- </Elements>
Step 9
Build and deploy the solution.
Step 10
The 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 11
The 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 12
The 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:
SummaryIn this article we saw have how to create an event receiver for a Document Library and custom list level in SharePoint 2013.