I have a local development environment in my VM.(SharePoint server 2013 SP1,Visual Studio Ultimate 2013-update3). I'm trying to add an event receiver to a document library on ItemDeleting. Deleting a document in the document library should add an item to my custom List 'Log'. Here is my code:
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 an 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 the following error:
"An error occurred in deployment step 'Activate Features': The list "Doclib" doesn't exist. Please fix the ListUrl attribute."
Please advise on the right approach. Why is my event receiver not firing?