SharePoint 2007 - EventHandling on Lists

We have been working with the latest Beta 2 of SharePoint 2007, and focusing on all the customizing that is SO much easier, in 2007 then in 2003.  The events are one of the much improved things in 2007, and I have been digging into them in the last week. I thought it might be helpful to post my findings, to see if it is useful to other people.

 

I went pretty thoroughly the events available on a list, creating a custom class that subclasses from the SPItemEventReceiver.  I didn’t touch the ContextMenu event, because I didn’t want to get into that yet. I did override all the other events, and tracked the order in which they fired.  There are some issues where the current SDK for the beta is not correct, and has sample code that doesn’t and can’t work.  I will go through that as well, but here are the results from the order of events, most are as you expected.

 

Order of Events

These results are using the SharePoint UI, I haven’t tracked the order of events on modifying lists programmatically yet.

 

 Action

Event Order

Add an item

ItemAdding

ItemAdded

Update an item

ItemUpdating

ItemUpdated

Add an item with an single attachment (if you add more then one attachment AttachmentAdding and AttachmentAdded will fire as many times as there are attachments)

ItemAdding

ItemAttachmentAdding

ItemAttachmentAdded

ItemAdded

Update an item with adding an Attachment

ItemUpdating

ItemAttachmentAdding

ItemAttachmentAdded

ItemUpdated

Update an item with deleting an Attachment

ItemUpdating

ItemAttachmentDeleting

ItemAttachmentDeleted

ItemUpdated

Checking a file in

ItemCheckingIn

ItemCheckedIn

Updating and Checking in a file from Word 2003 or 2007

ItemUpdating

ItemUpdated

ItemUpdating

ItemCheckingIn

ItemUpdated

ItemCheckedIn

 

 

Moving a file in a document library from one folder to another folder, using Network Share.

ItemAdding

ItemUpdating

ItemAdded

ItemUpdating

ItemUpdated

ItemUpdating

ItemUpdated

ItemDeleting

ItemDeleted

Creating a Folder in a List or Document Library

ItemAdding

ItemAdded

 

 

Those are the events that I have gone through so far. When moving a file while SharePoint Designer (Frontpage), the events don’t fire when moving files from folder to folder. 

 

The events ItemAdding, ItemAdded fire when adding a file using SharePoint Designer, and they do fire when updating a file, but just not when moving.

 

Updating an Item

 

After reading the SDK section, “Creating a Basic Event Handler”, I found that the documentation is just wrong.  The example at the end of the page –

 

public override void ItemAdded(SPItemEventProperties properties)

{

    properties.AfterProperties["Body"] = "Body text maintained by the system.";

}

 

It is just wrong, because the AfterProperties property is read only, and you can only get the properties, same with the BeforeProperties.  I tried to put in some basic auditing using an Updating or Updated event, write to a hidden column on the item, the contents of the changes.  I know there is the ChangeLog in SharePoint 2007, but I was curious to see how to do auditing with events.

 

Since, I couldn’t set the AfterProperties like the SDK explains, I simple update the item programmatically.  I thought it would create an infinite loop, because the event would fire and the item would update itself, and cause the event to fire again.  It didn’t behave that way, it fired 9 times and then stopped. I’m still researching, but it is very unusual. I plan to write some simple logic, that if only the hidden column is being updated then don’t update again, to get around the loop, but I’m not sure if Microsoft is planning to make the AfterProperties able to be set in Beta 2 – TR or if this is the way they are planning to leave it.

 

Well, I will go back to battling w/ the SharePoint 2007, and hammering away on events.  We should have a post about the “joy” of Workflow, soon as well.

 

Morgan



kick it on SharePointKicks.com

?>

posted @ Thursday, December 06, 2007 5:47 PM

Print