Wednesday, August 20, 2008

SharePoint: Register / Unregister Event Receivers

"Event Receivers" in SharePoint is one topic, I always wanted to discuss and today seems to be a good day to do so.Event Receivers allow you to add your custom/specific business logic/needs into the default functionality of SharePoint. As in, item Added, item Deleted, item Updated, site Created, site Deleted, etc... are few events SharePoint raises and carry out actions based on those events. Now, requirement may arise when, we need to do "something more" when such events are raised by SharePoint.
That "something more" can be:
  • Retrieve information from a database such as filling in the remaining fields of a list based on a CustomerID (assuming you don't have an enterprise license for BDC)
  • Call a web service
  • Start a workflow
  • Perform list data validation
  • Convert document to PDF and store in alternative list.
  • Access data in another list
  • or many other things.
To do this "something more" we need event receivers.

Question arises how does one hook/unhook custom logic to these events. For this refer the code below(code below operates on registering/unregistering events to a SharePoint Document Library):

eventType = SPEventReceiverType.ItemAdding,SPEventReceiverType.ItemDeleted etc...
assemblyName =
ClassName = NameSpace,ClassName

register events
[code]
public static void addEventReceiver(SPList docLib, SPEventReceiverType eventType, string assemblyName, string className)
{
try
{
docLib.EventReceivers.Add(eventType, assemblyName, className);
docLib.Update();
}
catch(Exception ex)
{
// handle or raise exception
}
}
[/code]

un-register events
[code]
public static void removeEventReceivers(SPList docLib, SPEventReceiverType eventType, string assemblyName, string className)
{
try
{
int i;
for (i = 0; i < docLib.EventReceivers.Count; i++)
{
if (docLib.EventReceivers[i].Assembly.Equals(assemblyName)
&& docLib.EventReceivers[i].Class.Equals(className)
&& docLib.EventReceivers[i].Type.Equals(eventType))
{
docLib.EventReceivers[i].Delete();
}
} // looping thru event receivers.
docLib.Update();
}
catch(Exception ex)
{
// handle or raise exception
}
}
[/code]

Here is a list of events on which you can hook your custom code into:

Microsoft.SharePoint.SPWebEventReceiver : "Site Level"

SiteDeleted

Occurs after a site collection has been deleted.

SiteDeleting

Occurs when a site collection is being deleted.

WebDeleted

Asynchronous Afterevent that occurs after an existing Web site is completely deleted.

WebDeleting

Synchronous before event that occurs before an existing Web site is completely deleted.

WebMoved

Asynchronous after event that occurs after an existing Web site has been moved.

WebMoving

Synchronous before event that occurs before an existing Web site has been renamed or moved to a different parent object.

· Microsoft.SharePoint.SPListEventReceiver : "List Level"

FieldAdded

Occurs after a field link is added.

FieldAdding

Occurs when a field link is being added to a content type.

FieldDeleted

Occurs after a field has been removed from the list.

FieldDeleting

Occurs when a field is in the process of being removed from the list.

FieldUpdated

Occurs after a field link has been updated

FieldUpdating

Occurs when a field link is being updated

· Microsoft.SharePoint.SPItemEventReceiver : "List Item Level"

ItemAdded

Asynchronous After event that occurs after a new item has been added to its containing object.

ItemAdding

Synchronous before event that occurs when a new item is added to its containing object.

ItemAttachmentAdded

Asynchronous after event that occurs after a user adds an attachment to an item.

ItemAttachmentAdding

Synchronous before event that occurs when a user adds an attachment to an item.

ItemAttachmentDeleted

Asynchronous after event that occurs when after a user removes an attachment from an item.

ItemAttachmentDeleting

Synchronous before event that occurs when a user removes an attachment from an item.

ItemCheckedIn

Asynchronous after event that occurs after an item is checked in.

ItemCheckedOut

Asynchronous after event that occurs after an item is checked out.

ItemCheckingIn

Synchronous before event that occurs as a file is being checked in.

ItemCheckingOut

Synchronous before event that occurs after an item is checked out.

ItemDeleted

Asynchronous after event that occurs after an existing item is completely deleted.

ItemDeleting

Synchronous before event that occurs before an existing item is completely deleted.

ItemFileConverted


ItemFileMoved

Occurs after a file is moved.

ItemFileMoving

Occurs when a file is being moved.

ItemUncheckedOut

Synchronous before event that occurs when an item is being unchecked out.

ItemUncheckingOut

Synchronous before event that occurs when an item is being unchecked out.

ItemUpdated

Asynchronous after event that occurs after an existing item is changed, for example, when the user changes data in one or more fields.

ItemUpdating

Synchronous before event that occurs when an existing item is changed, for example, when the user changes data in one or more fields.



Cheers,
Sandeep

No comments: