locked
Binding an event handler to a list RRS feed

  • Question

  • Hi,

    I am trying to bind an event handler to a list using the following code. I have the same code before with success, but I am not sure why it is not binding it anymore. Also I am getting no error, but the event is not firing when I bind the event handler using the following code:

     

    static void Main(string[] args)
        {
          int check = 1;
          
          try
          {
            
            
            SPSite collection = new SPSite("https://fullsitepathhere");
            
            Console.WriteLine("Working - 1");
            check = 3;
            SPWeb site = collection.OpenWeb();
            Console.WriteLine("Working - 2");
            check = 4;
            SPList temp = site.Lists["Data Storage"];
            Console.WriteLine("Working - 3");
            
            check = 2;
    
            string assname = "NewSaas, Version=1.0.0.0, Culture=neutral, PublicKeyToken=038de5f4e8f782c2";
            string classname = "NewSaas.saasNew";
    
            temp.EventReceivers.Add(SPEventReceiverType.ItemAdded, assname, classname);
            Console.WriteLine("Working - 4");
            temp.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assname, classname);
            Console.WriteLine("Success!");
          }
          catch (Exception ex)
          {
            Console.WriteLine("Encountered this - " + ex.ToString() + " --> " + check.ToString());
            Console.ReadLine();
          }
    
    Any suggestions???

    Thursday, October 21, 2010 7:41 PM

Answers

All replies

  • I tried listing if any event handlers were registered to the list and it shows the assembly name for my module but the public key token is different than what I specify.

     

    Does anyone have any idea about this?

    Thursday, October 21, 2010 8:35 PM
  •     static void Main(string[] args)
        {
          string url = "https://fullsitepathhere/Data Storage/";
          string asmName = "NewSaas, Version=1.0.0.0, Culture=neutral, PublicKeyToken=038de5f4e8f782c2";
          string className = "NewSaas.saasNew";
    
          //Register Event Receiver
          using (SPSite siteCollection = new SPSite(url))
          {
            using (SPWeb web = siteCollection.OpenWeb())
            {
              SPList lst = web.GetList(url);
              if (lst != null)
              {
                bool isRegistered = false;
                SPEventReceiverDefinitionCollection eventReceivers = lst.EventReceivers;
                foreach (SPEventReceiverDefinition edef in eventReceivers)
                {
                  if (edef.Assembly.Contains(asmName) && edef.Class.Contains(className))
                  {
                    isRegistered = true;
                    Console.WriteLine("The event Handler {0} is already registered.", asmName);
                    break;
                  }
                }
                if (!isRegistered)
                {
                  SPEventReceiverDefinition eventReceiver = eventReceivers.Add();
                  eventReceiver.Name = "saasNew";
                  eventReceiver.SequenceNumber = 1;
                  eventReceiver.Type = SPEventReceiverType.ItemAdded;
                  eventReceiver.Assembly = asmName;
                  eventReceiver.Class = className;
                  eventReceiver.Update();
                }
              }
              else
              {
                Console.WriteLine("Error. Url should contain a fill path to the list or document library.");
              }
            }
          }
          Console.WriteLine("The event Handler {0} has been registered.", asmName);
          Console.ReadLine();
     }
    }
    

     

     


    Oleg
    Thursday, October 21, 2010 8:36 PM
  • Hi Oleg,

     

    Thanks for your response. The code gave the that my event handler is registered, but the problem is that when I add an item to the list the event does not fire. Any suggestions?

    Thursday, October 21, 2010 10:33 PM
  • I did some debugging and found out that binding still does not work for other lists on the same site, but binding works for lists on other sites. Is there some configuration for the site that needs to be set in order for the events to fire?
    Friday, October 22, 2010 1:33 AM
  • Hi Malhar,

     

    Thanks for your post.

    You can try to delete the event receiver from the site, and then register it  to the site again.

    SPEventReceiverDefinition.Delete

    Deletes the event receiver registration from the SPEventReceiverDefinitionCollection object.

     

    Share your result.


    SharePoint 2010
    Friday, October 22, 2010 6:29 AM
  • I tried delete and re-register a couple of times but still it didn't work.

    I am confused since this thing happens for another list (which i created for testing) on this site, but this problem does not occur when I did the same process on another list on a different site.

    Friday, October 22, 2010 5:20 PM
  • Try SharePoint Manager 2007 http://spm.codeplex.com/. This tool will help you to figure out what is wrong.
    Oleg
    Friday, October 22, 2010 5:48 PM
  • Hi Malhar,

     

    Glad to receive your reply.

     

    You can also use a tool such as the Event Handler Explorer to view the event handlers assigned to the list:

    http://www.u2u.info/SharePoint/U2U%20Community%20Tools/EventHandlerExplorer.zip

     

    Here is a similar issue, hope it’s helpful.

    http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/f9b1fa0a-d7c4-467d-8d4b-bf8fce99af3c


    SharePoint 2010
    • Proposed as answer by AnilChitatil Thursday, October 28, 2010 1:36 PM
    • Marked as answer by Aaron Han - MSFT Sunday, October 31, 2010 6:50 AM
    Tuesday, October 26, 2010 2:54 AM
  • Hi Malhar,

    http://experionsharepointsolutions.blogspot.com/

    see this for create an event handler. Here i mentioned step by step procedure to adding an event handler for a document library.

    I hope this will help you.

    Thanks and Regards,

    Anil

    • Proposed as answer by AnilChitatil Thursday, October 28, 2010 1:47 PM
    • Marked as answer by Aaron Han - MSFT Sunday, October 31, 2010 6:50 AM
    Thursday, October 28, 2010 1:47 PM