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
Delete Document:
Step 8Here targeting to add the event receiver only to the “EmployeeDocuments” library, the Elements.xml file requires the following change.

Step 9Build and deploy the solution.
--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:
- Name
- Modified
- ModifiedBy
An EmployeeDocumentLog List has the following columns:
- Title
- 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.
Add Document
Here note that the ItemAdded method is used instead of the ItemAdding method.
- public override void ItemAdded(SPItemEventProperties properties) {
- //base.ItemAdding(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) {
- //base.ItemUpdating(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;
- }
- }
- }
- public override void ItemDeleting(SPItemEventProperties properties)
- {
- //base.ItemDeleting(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;
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
- <!--<Receivers ListTemplateId="101">-->
- <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 10The document has been added.
Go to the EmployeeDocuments Document Library and add a new document.
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.
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 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














