65.9K
CodeProject is changing. Read more.
Home

ManageEventHandler

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (2 votes)

Feb 28, 2008

CPOL

1 min read

viewsIcon

18998

downloadIcon

87

A Windows application to register an event handler for a desired list only.

Introduction

In WSS 2007, event handlers have been improved a lot, and it allows us to extend our SharePoint environment in a lot of ways. Events are fired not only in document libraries but also in lists - and even at various other levels (site and web level). It is also now possible to hook up multiple event handler assemblies to our list or library.

For ease of understanding, I'm using a tree structure. The user can traverse the tree and find the appropriate list for attaching the EH. :)

Using the code

The code is pretty simple and easy to understand. CreateTree() is a recursive method which will fill the tree with all its child nodes including the lists under that particular site.

public void GetTreeView()
{

    SPWebApplication wa = 
      SPWebApplication.Lookup(new Uri(textBoxSiteCollectionURL.Text));
    SPSiteCollection wasites = wa.Sites;

    //SPWebCollection sub_SubWeb;
    System.Windows.Forms.TreeNode rootnode;
    //System.Windows.Forms.TreeNode firstchild;
    rootnode = new System.Windows.Forms.TreeNode(wa.Name);
    treeView1.Nodes.Add(rootnode);

    foreach (SPSite site in wasites)
    {
        SPWeb web = site.RootWeb;
        CreateTree(web, rootnode);
        GetList(web, rootnode);
    }
    //For writing treeview in xml file.....
    TreeViewSerializer serializer = new TreeViewSerializer();
    serializer.SerializeTreeView(this.treeView1, 
      Application.StartupPath + "\\..\\..\\bhanu.xml");
}

How to use the utility

  1. Provide the URL of the web application.
  2. Click the "Explore" button.
  3. Navigate the tree view of the Site collection.
  4. Select the List node.
  5. Click the "Load Assembly" button and browse the assembly (Note: The assembly should be GACed first).
  6. Select the class name from the drop-down combo.
  7. Provide the sequence number.
  8. Select the event type from the drop-down combo.
  9. Click "Add Assembly".
  10. For removing the event handler, we need to select the relevant event handler node from the tree, if there is any event associated with it. Then, simply click the "Remove Handler" button.

Caution!!!

If you are getting an Access Denied message after clicking the "Explore" button, then you are probably not running the RegisterEventHandler app in the context of a Site Collection Administrator account. Go to Central Administration -> Application Management -> Site Collection Administrators, and add yourself as the secondary site collection administrator. Be sure that you add yourself to the correct site collection.

Happy SharePointing.... :)

Cheers!!!