Select the type of event receiver you need to add, and select the events you need to handle.
In this sample I'm trying to update a SharePoint list based on file changes happening to a separate SharePoint library. Basically, the list will act like a log. So we need to create the library and a list. Here, I have created the Department library to add and maintain documents and also created DocumentLog list to log the changes happening to the library.
In the list I have three columns, Title, Action & DateAndTime in order to catalog the changes happening to the library.
[The created document library and list]
public override void ItemAdded(SPItemEventProperties properties)
{
//base.ItemAdded(properties);
using (SPWeb web = properties.OpenWeb())
{
try
{
SPList list = web.Lists["DocumentLog"];
SPListItem newItem = list.Items.Add();
newItem["Title"] = properties.ListItem.Name;
newItem["DateAndTime"] = System.DateTime.Now;
newItem["Action"] = "Item Added";
newItem.Update();
}
catch (Exception ex)
{
throw ex;
}
}
}
public override void ItemUpdating(SPItemEventProperties properties)
{
//base.ItemUpdating(properties);
using (SPWeb web = properties.OpenWeb())
{
try
{
SPList list = web.Lists["DocumentLog"];
SPListItem newItem = list.Items.Add();
newItem["Title"] = properties.ListItem.Name;
newItem["DateAndTime"] = System.DateTime.Now;
newItem["Action"] = "Item 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["DocumentLog"];
SPListItem newItem = list.Items.Add();
newItem["Title"] = properties.ListItem.Name;
newItem["DateAndTime"] = System.DateTime.Now;
newItem["Action"] = "Item Deleted";
newItem.Update();
}
catch (Exception ex)
{
throw ex;
}
}
}
As I am targeting to add the event receiver only to the Department document library, the Elements.xml file requires a change.
Note that I have commented out the setting which points to all document libraries, instead pointed to Department document library. The edited Elements.xml file is as below:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- <Receivers ListTemplateId="101"> -->
<Receivers ListUrl="Department">
<Receiver>
<Name>DepartmentEventReceiverItemAdded</Name>
<Type>ItemAdded</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>MySharePointProject.ListEventReceiver.DepartmentEventReceiver</Class>
<SequenceNumber>10000</SequenceNumber>
</Receiver>
<Receiver>
<Name>ListEventReceiverItemUpdating</Name>
<Type>ItemUpdating</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>MySharePointProject.ListEventReceiver.DepartmentEventReceiver</Class>
<SequenceNumber>10000</SequenceNumber>
</Receiver>
<Receiver>
<Name>ListEventReceiverItemDeleting</Name>
<Type>ItemDeleting</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>MySharePointProject.ListEventReceiver.DepartmentEventReceiver</Class>
<SequenceNumber>10000</SequenceNumber>
</Receiver>
</Receivers>
</Elements>
Compile and deploy the solution to your site. Now you may play around with the library and observe the changes happening to the list.... :)
I have added a document, uploaded, added, modified the 3 docs and then deleted one of the docs respectively.
And here's what I get in the DocumentLog list.