Manually create and attach an SpListItem's Event Handler to a SharePoint 2007 SpList

Tuesday, 24 March 2009 21:53 by myro


Open your Visual Studio and create a new Project as Class Library. Add a reference to "Microsoft.SharePoint.dll", rename Class1.cs to HelloHandler.cs and paste:

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

 

namespace Myrocode.Templates

{

    public class HelloHandler : SPItemEventReceiver

    {

        public override void ItemAdding(SPItemEventProperties properties)

        {

            properties.AfterProperties["Title"] = "Hello event handler world !";

        }

    }

}

Build the Assembly, sign it (strong naming) and deploy it in your GAC (Global Assembly Cache - "C:\WINDOWS\assembly"). In the GAC window, get your DLL's Version and Public Key Token's proprieties and write them down. You can use Reflector to simplify this task.
Now you have to attach your custom event handler to a specific list: create a console application project in Visual Studio and paste this snippet:

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

 

namespace Myrocode.Templates

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                string siteURL = args[0];

                string listName = args[1];

                using (SPSite site = new SPSite(siteURL))

                {

                    using (SPWeb web = site.OpenWeb())

                    {

                        SPList list = web.Lists[listName];

                        list.EventReceivers.Add(SPEventReceiverType.ItemAdding,

                            "Myrocode.Templates.Handlers, Version=1.0.0.0," +

                            "Culture=neutral," +

                            "PublicKeyToken=78fde53655a71179",

                            "Myrocode.Templates.Handlers.HelloHandler");

                        list.Update();

                        Console.WriteLine("Handlers added correctly!");

                    }

                }

                Console.WriteLine("press any key to exit ...");

                Console.Read();

            }

            catch (Exception ex)

            {

                Console.WriteLine("Error! stack:" + ex.StackTrace);

                Console.WriteLine("Error! message:" + ex.StackTrace);

                Console.WriteLine("press any key to exit ...");

                Console.Read();

            }

        }

    }

Remember to insert your custom properties where you see highlighed code lines.

Credits for this code snippet goes to Peppedotnet.

UPDATE 

More considerations should be done to the update method of a SPListItem.
Let's says that you need to update a SpListItem by adding a new value into the Title field. Here's how the Best Practice indicates should be done:

"Courier New">listItem[listItem.Fields["Title"].InternalName] = “myrocode's handler”;
// IMPORTANT perform the update by disabling the event firing!!
this.DisableEventFiring();
listItem.SystemUpdate(false);
this.EnableEventFiring();

An another tool that can be installed into SharePoint 2007 as a feature is http://www.codeplex.com/speventsmanager, which allows you to manage all the event handlers attached to a SharePoint List directly from your browser.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   SharePoint 2007
Actions:   Bookmark and Share | Permalink | Comments (2) | Comment RSSRSS comment feed
If you consider this post usefull for your purposes, please consider visiting my sponsors to help me out with the myrocode.com maintenance. Thank you.

Comments