Monday, March 15, 2010

ItemEvent listener would not fire on a list

I created some list item event receivers in Visual Studio 2008 with SharePoint Extensions. After installing the solution and activating features both at the web level and also site level, the list item events would not fire.

Reason why the listeners are not fired is because even after feature is created, the listeners are not hooked up to the list.

I did a small one timer console application to do this for me. Here is the code.

       static void Main(string[] args)
        {
            System.Console.WriteLine("Starting....");

            SPSite _site;
            SPWeb _web;

            _site = new SPSite("http://sitelocation/");
            System.Console.WriteLine("Successfully connected.");

            _web = _site.OpenWeb();

            SPList _list = _web.Lists["Specify list name here"];

            System.Console.WriteLine("Connected to list");

            string assemblyName = "AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=AssemblyToken";
            string className = "Namespace.ClassName";

            _list.EventReceivers.Add(SPEventReceiverType.ItemAdded, assemblyName, className);
            _list.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assemblyName, className);
            _list.EventReceivers.Add(SPEventReceiverType.ItemUpdating, assemblyName, className);

            System.Console.WriteLine("Receivers added to the list.");
            System.Console.WriteLine("Hit  to continue ...");
        }

No comments:

Post a Comment