Reputation: 11
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.
Upvotes: 1
Views: 4717
Reputation: 21
DO you want this event reciever to fire for all the document libraries. if no ,the best thing to do is to register for the event reiver in the feature activated section of the sharepoint project .
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb oWeb = (SPWeb)properties.Feature.Parent;
SPList list=oWeb.List["ListNameorDocLibName"];
list.EventReceivers.Add(_theeventRecieverType, Assembly.GetExecutingAssembly ().FullName, "EventReceiverProject1.EventReceiver1.EventReceiver1");
list.Update();
}
If you do this way , you dont need to worry about the URL and event reciever will definelty fire.
Upvotes: 1
Reputation: 2654
I think there are two things you need to solve in this case,
Another thins is you have commented out the line //base.ItemDeleting(properties);
please uncomment that one it may fix your issue.
For the second question,
It will require the URI for the list not the list name generate the path for the list and provide it and then it will work.
Let me know the outcome
Thanks
Upvotes: 0