Downloads

Registering SharePoint 2007 site event receivers - the SEventReg tool

February 12, 2007

The amount of control over events has increased in MOSS 2007. It's now possible to cancel actions before they are completed. The names of such events, such as WebDeleting, always end in the suffix "ing". These events are executed synchronously.

Other events, for example WebDeleted, indicate that some type of actions has already occurred. Those events always end with the suffix "ed" and are processed asynchronously.

The following code listing provides a simple example of an event receiver cancelling the deletion of a SharePoint site (SPWeb object):

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace MyNamespace.MyEventReceiver
{
  public class StopDeleteAction : SPWebEventReceiver
  {
    public override void WebDeleting(SPWebEventProperties properties)
    {
      properties.Cancel = true;
      properties.ErrorMessage = "It's not allowed to delete this site";
    }
  }
}

Event receivers need to be registered in the GAC, so they need to be strong named. Check out the article establishing the public key token if you want to learn more about strong naming. In this post, we won't discuss how to do this. After installing an event receiver in the GAC event receivers need to be activated within SharePoint.

Event receivers handling events associated to lists can be deployed via features. We won't discuss that either. Other event receivers need to be installed via the SharePoint object model, and we've created a simple tool called the SharePoint Event Receiver Registration tool (seventreg.exe).

Seventreg is available for download and can be used to install event receivers for events associated to SharePoint sites, which are the following events:

  • WebDeleting
  • WebMoving
  • WebDeleted
  • WebMoved

The source code is also available and is easy to modify if you want to support other event types as well. Seventreg can be used to enumerate a list of existing event receivers for a given SharePoint site, it can be used to add event receivers and it can also be used to delete event receivers.

The code for enumerating existing event receivers for a given SharePoint site looks something like this:

using (SPSite site = new SPSite(strUrl))
{
  using (SPWeb web = site.OpenWeb())
  {
    SPEventReceiverDefinitionCollection coll = web.EventReceivers;

    if (coll.Count == 0)
    {
      Console.WriteLine("0 event receivers registered at " + strUrl);
    }

    foreach (SPEventReceiverDefinition def in coll)
    {
      Console.WriteLine("Event receiver: " + def.Name);
    }
  }
}

The next code listing shows how to add an event receiver for a given SharePoint site:

using (SPSite site = new SPSite(strUrl))
{
  using (SPWeb web = site.OpenWeb())
  {
    SPEventReceiverDefinitionCollection receivers = web.EventReceivers;

    SPEventReceiverDefinition def = receivers.Add();
    def.Name = "MyEventReceiver";
    def.Assembly = "MyEventReceiver, Version=1.0.0.0, Culture=neutral,
    PublicKeyToken=[public key token]";
    def.Class = "MyEventReceiver.StopDeleteAction";
    def.Type = SPEventReceiverType.WebDeleting;
    def.SequenceNumber = 10020;
    def.Update();

    Console.WriteLine("Registration of event " + def.Name + " for url "
    + strUrl + " successful");
  }
}

The next code listing shows how to delete an event receiver for a given SharePoint site:

using (SPSite site = new SPSite(strUrl))
{
  using (SPWeb web = site.OpenWeb())
  {
    SPEventReceiverDefinitionCollection coll = web.EventReceivers;
    foreach (SPEventReceiverDefinition def in coll)
    {
      if (def.Name == strName)
      {
        def.Delete();
        Console.WriteLine("Deleted event receiver " + strName
        + " from site " + strUrl);
        break;
      }
    }
  }
}

The seventreg tool is a command line tool that makes enumerating, adding and deleting real easy. Use the following command to learn more about the seventreg tool:

seventreg -help

Use the following command to enumerate all event receivers for a given web site:

seventreg -enum -url http://my.site.url/web1

Use the following command to add a new event receiver for a given web site:

seventreg -add -url [site url] -config [path to config file]

The config file contains information needed for the event receiver registration and looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<Config>
  <!--
  MyWebEventReceiver
  -->
  MyEventReceiver

  <!--
  MyWebEventReceiver, Version=1.0.0.0, Culture=neutral,
  PublicKeyToken=[public key token]
  -->
  MyEventReceiver, Version=1.0.0.0, Culture=neutral,
  PublicKeyToken=[public key token]


  <!--
  MyNamespace.MyClass
  -->
  MyEventReceiver.StopDeleteAction

  <!--
  WebDeleting = 202
  WebMoving = 203
  WebDeleted = 10202
  WebMoved = 10203
  -->
  202

  <!--
  10000
  -->
  10020
</Config>

Adjust this file according to your needs. The final command shows how to delete a specific event receiver for a given SharePoint site:

seventreg -del -url [site url] -name [name of event receiver]

You can download seventreg here. You can download the source code here.

Summary

If you want to install event receivers via the SharePoint object model the seventreg tool might be a good starting point for you. If you need to install event receivers associated to SharePoint sites, feel free to use the seventreg functionality as is. If you want to learn more about SharePoint 2007 events we would advise you to read the following book: Developer's Guide to WSS 3.0.

« back to overview page