Skip Navigation Links
Level Orange > Posts > Building MOSS Item Event Handlers
Building MOSS Item Event Handlers

Intro

So you have a list in MOSS, now what? I want to have a unique index field or I want to update contents of the list with other lists, or I want to start a process, etc. To extend a list, one of the many things you can do is to build an event handler for that list. Similar to other .NET objects, a SharePoint List has Event Handlers that can be tied to certain actions triggered by usage of that object. It is important to know that there are two kinds of execution patterns depending on the event that is coded for.

Synchronous Event Handlers:

The first is synchronous event handlers. These would be where you could stop the execution of the thread before completion. If you take a look at the Item Adding event for instance, you could check to see if you are violating a specific business constraint and stop the execution or add process if the validation fails. Pretty much a rule of thumb on these is that they all end in "ing" so Item Adding, Item Updating, Item Deleting, etc. Think of the power of this if I get to write any .NET code on this particular delegate.

Asynchronous Event Handlers:

The second would be asynchronous event handlers. These would be when a new thread would be started to run this code. If you take a look at Item Updated, we could possibly retrieve data from another list and cross populate into your list based on a lookup field or any number of items. This is the exact sample that we will be addressing in this article.

Writing the code:

The first thing we will do is open visual studio and create a new project. It can be a simple class library:

We are then going to add the Microsoft.SharePoint.dll as a reference:

Now we are ready to write our code. Remember for this that we are going to simply take the contents of one list and based upon a lookup value, populate based on contents of another list.

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

namespace MSIT.ItemReceivers

{

class UpdateContactsEvent : SPItemEventReceiver

{

public override void ItemUpdated(SPItemEventProperties properties)

{

UpdateContents(properties);

}

public override void ItemAdded(SPItemEventProperties properties)

{

UpdateContents(properties);

}

void UpdateContents(SPItemEventProperties properties)

{

using (SPWeb spWeb = properties.OpenWeb())

{

//Get Lookup List

SPList ContactType = spWeb.Lists["ContactType"];

//Get List Item to be updated

SPListItem Contact = properties.ListItem;

string sContactType = string.Empty;

sContactType = Contact["ContactType"].ToString();

//Get ListItem of Lookup List

SPListItem ContactTypeItem = null;

foreach (SPListItem ContactTypeLoop in ContactType.Items)

{

//We are doing a contains because this is an array lookup but we could explicitly do something like:

//If (ContactTypeLoop["ContactType"].ToString() == Contact["ContactType"].ToString().Split(';')[1].Replace('#','')

if (sContactType.Contains(ContactTypeLoop["ContactType"].ToString()))

{

ContactTypeItem = ContactTypeLoop;

break;

}

}

if (ContactTypeItem == null)

{

throw new Exception();

}

//Update Original ListItem

Contact["ExtraData"] = ContactTypeItem["SendChristmasCards"].ToString();

Contact.Update();

}

}

}

}

     

Build and GAC the Receiver

What you are going to have to do now is GAC the assembly. Instructions can be found in my blog posting here.

Associate Receiver to List

Finally, the moment of truth, let's attach the code to the list that we want. There are two main ways we can accomplish.

Code Approach

The instructions for Code Approach can be found here.

Feature Approach (Recommended)

The instructions for Feature Approach can be found here.

Comments

There are no comments yet for this post.