I have a local development environment in my VM.(SharePoint server 2013 SP1,Visual Studio Ultimate 2013-update3). I'm trying to add a event receiver to a document library on ItemDeleting.Deleting a document to doc lib would add item to my custom List 'Log'.Here goes my coding:
Event.cs
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace SharePointProject1.EventReceiver1
{
public class EventReceiver1 : SPItemEventReceiver
{
public override void ItemDeleting(SPItemEventProperties properties)
{
//base.ItemDeleting(properties);
using (SPWeb web = properties.OpenWeb())
{
try
{
SPList list = web.Lists["Log"];
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;
}
}
}
Element.xml
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- <Receivers ListTemplateId="101" > -->
<Receivers ListUrl ="Doclib"
<Receiver>
<Name>EventReceiver1ItemDeleting</Name>
<Type>ItemDeleting</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>SharePointProject1.EventReceiver1.EventReceiver1</Class>
<SequenceNumber>10000</SequenceNumber>
</Receiver>
</Receivers>
</Elements>
Feature1.Template.xml
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/">
</Feature>
I deployed the solution on the SP site and activated the feature.
So i have two questions,
When i use the code- ListTemplateId="101" ,i do not get any error.But the event receiver is not firing. When i delete,the "Log" list is not updated. Am i missing something?
If i give the Document library name in the ListUrl,i get an error
"An error occurred in deployment step 'Activate Features': The list "Doclib" doesn't exist.Please fix the ListUrl attribute.
Please advice on the right approach. Why is the Event Receiver not firing?
Thanks in advance.