2

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:

  1. 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?
  2. 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?

2
  • listtemplateid = 100
    – Nikhil J
    Sep 11, 2014 at 17:39
  • I guess 100 is for list and not document library.Here i have document library.
    – leo
    Sep 11, 2014 at 20:53

3 Answers 3

1

There are many reasons why an EventReceiver would not fire. According to my experience, one of the sneakiest reasons is that you are using a dll from another project, but, from the same solution. And if you didn't add the assembly for when the solution is deployed the EventReceiver using it would not fire because the dll would not load.

If you haven't tried that out yet, it's like this:

In your solution, double-click Package -> Then Add the assembly for the dll you want to use.

enter image description here

1

One issue I've had with event receivers is them not attaching to a list properly. As a result I've had to attach them manually through feature receivers Activate function with this code

<SPList Object>.EventReceivers.Add(SPEventReceiverType.ItemDeleting, Assembly.GetExecutingAssembly().FullName, "SharePointProject1.EventReceiver1");

With event receiver code something like this

public override void ItemDeleting(SPItemEventProperties properties)
{
  EventFiringEnabled = false;
  /*

    Your Code Here

  */
  base.ItemDeleting(properties);
  EventFiringEnabled=true;
}

With this approach, attaching is done via code, and Elements.xml is not needed, and the event receiver is attached only to a specific SPList Object.

It is a good idea to have it removed via code on feature deactivate if the list is not being deleted.

0

You can follow a different approach .Rather than specifying list id or name in ELement.xml you can specify same thing in the code

public override void ItemDeleting(SPItemEventProperties properties)
    {
     if(properties.ListItem.Title="Name of your list")
{
        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;
            }
        }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.