Sharing SharePoint Knowledge

Technical Solutions for SharePoint Server 2010

Archive for the ‘Event Model’ Category

Events Improvements in SharePoint 2010

Posted by Chandrasekaran C N on June 7, 2010

Hi All,

In MOSS 2007, there are a lot of events introduced by Microsoft such as list item events, list events, field events, etc., which are very useful to execute some tasks on the lists based on the user inputs. But there are some limitations in the event model in MOSS 2007, which are necessary for the developers. To overcome those limitations developers need to do some workarounds.

Following are some of the limitations

  • Cannot hook the events to a particular SharePoint lists
  • Need to use standard pre-defined error page when canceling the events, which is not so intuitive for the end users
  • No javascript alert instead of redirecting to pre-defined error page
  • No events for SPWeb object

Microsoft addressed some of the limitations and also introduced new events in SharePoint 2010. Here are the list of events introduced in SharePoint 2010

Name of the events Description
WebAdding Synchronous event, fired before the web is added. Some of the properties such as URL will not be visible in this event, since the web is not yet created
WebProvisioned Either synchronous or asynchronous after-events fired after web is created. We can make this event either synchronous or asynchronous by setting the Synchronization property to either Synchronous or Asynchronous respectively. This property is in elements.xml under <Register> node
ListAdding Synchronous event, fired before a list is created. 
ListAdded Either synchronous or asynchronous after-events fired after list is created before it is presenting to the user.
ListDeleting  Synchronous event, fired before a list is deleted.
ListDeleted Either synchronous or asynchronous after-events fired after list is deleted.
These events are created by using the same procedure as we creating events in MOSS 2007. Using Visual Studio 2010, developers are able to write,  deploy and debug these events in SharePoint server easily. Visual Studio 2010 provides a pre-defined project templates to create event receivers.
 
Enhancements in SharePoint 2010
 Custom Error Pages: SharePoint 2010 provides developers to create/redirect to custom error pages when the events are cancelled.  This gives developers a great flexibility to provide more information on the custom error pages.  The redirection will applicable only for the pre-synchronous events and not for post-synchronous events like ListAdded, etc.,  Below shows the code snippets to redirect to custom error page
           properties.Cancel = true;
           properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
           properties.RedirectUrl = "/_layouts/CustomErrors/error.aspx";
 
Event Registration Feature: With new event registration enhancement, developers are able to hook the event to a particular SharePoint list in the elements.xml file using ListUrl node by passing relative url of the list.  Developers are now able to scope the events either at Site or Web levels and also set the property RootWebOnly in Receivers node, if the event receiver only works for root site.
Impersonation Improvements:  Developers are able to elevate the privileges in the event receivers in order to perform some tasks, which current user who triggered the event does not have adequate permissions to do so using SPSecurity.RunWithElevatedPrivileges method.  In SharePoint 2010, there are new properties namely OriginatingUserToken, UserDisplayName and UserLoginName which help the developers to revert back to the original user who triggered the event very easily.
Thanks
Chandrasekaran C N

Posted in Event Model, SharePoint, SharePoint 2010 | Tagged: , , | Leave a Comment »