Saturday, May 8, 2010

Some new and enhanced events in SharePoint 2010

Hi all,

The new AVATAR of SharePoint 2010 comes with some exciting and enhanced events for the developers.

New Events and Registration capabilities:
It comes with:
  1. New events such as ListAdding, ListAdded, ListDeleting, ListDeleted etc which were NOT available in SharePoint 2007.
  2. New registration capabilities with the feature schema. We have something called "scope" mechanism where we can apply the event to site collection level (SPWeb) or on to the web level (SPWeb). 
Post Synchronous events: (the "-ed" event) 
In SharePoint 2007, we used to have asynchronous events such as ItemAdded, ItemUpdated event and these events occurs or fires "after" a new item has been added to its containing object. Apparently, if want to run some post analysis but we want to hold up the user till we redirect to the page. It impossible in SharePoint 2007 version as its already committed to the content database. 

In SharePoint 2010, its possible and we can leverage a new property named: SPEventReceiverDefinition.Synchronization 

New cancel event and create custom error pages:
In SharePoint 2007, we can leverage some kind of business logic and we are able to cancel the event (only in synchronous -ing events). But the problem, SharePoint always uses layouts pages which is the standard look and feel. We did not have much control over it.  

In SharePoint 2010, we can cancel the event and we have a new functionality through which we can create our own custom error page.

Note: This functionality is only on browser based experience and it will NOT work on office clients.

Visual Studio 2010 tools for SharePoint: 
Developers can use VS 2010 tools for developing event handlers leveraging out-of-the-box SharePoint tools. We will walk through be creating a brand new event handler using VS 2010 tools.  When developer creates a new event VS 2010 asks him about what kind of item or list we want to create in English sense which is easier for understand.There are around 12 new event handlers.

Lets jump into a simple walk through.

Step1: Create a new Visual Studio 2010 >> Choose "SharePoint 2010" templates >> Choose "EventReceiver" >> Type the project name as "CustErrPage" and Click OK button. 

Step2: The next screen in the wizard, we have to choose the farm solution for debugging. Click Next button.

Step3: The next screen in the wizard, we have to choose event receiver settings. As we can observe that there are new events such as ListAdding, ListAdded etc. We will choose ItemAdding event for demo purpose. The following screen shots shows the new events:

New list events
New List Item Event
New List Email Event

New Web Events
New List Workflow Events
Step4: We add a simple code for the announcements list and if the "Title" contains "throw error" then we cancel the event.   
public override void ItemAdding(SPItemEventProperties properties)

           if(properties.AfterProperties["Title"].ToString().ToLower().Contains("throw error"))
               properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
               properties.RedirectUrl = "/_layouts/CustErrPage/ErrorPage.aspx";
Step5: Right click on the project New >> New Item >> choose the "Application Page" >> Name: ErrPage.aspx. In the "PlaceHolderMain" we add a simple error message for the user.
"YOu cannot add new announcement."

Step6: Deploy the solution. Go to the SharePoint 2010 site >> Create a new announcement and in Title field type in "throw error" >> Click Save button.

Step7: A custom error will pop up which we had designed.

Note: In one of the posts, I had to use "vti_title" in the "Title" field for properties.AfterProperties["vti_title"](SP 2007). Apparently, in SharePoint 2010, properties.AfterProperties["Title"] works normally.


Download the source here

    No comments:

    How to: Use cascading drop-down lists in PowerApps

    Hi all, Using cascading dropdown, users can easily fill the forms by selecting drop-down values dependent on values from another dro...