RSS Feed Feed your read!

Bookmark and Share







Tag Cloud

ASP.NET Generic, Best Practices, Business Intelligence, Freeware Releases, InfoPath, Infrastructure, jQuery, Lunch & Learn Events, Project Server, Random, Reporting Services, Search, SharePoint Administration, SharePoint Business Analysis and Project Management, SharePoint Development, Silverlight, Social Networking, Speaking Events, White Paper Releases, Workflow Foundation,

Archives

June 2007 (3)
August 2007 (1)
November 2007 (2)
February 2008 (2)
April 2008 (5)
May 2008 (7)
June 2008 (8)
July 2008 (7)
August 2008 (3)
September 2008 (7)
October 2008 (1)
November 2008 (3)
December 2008 (3)
January 2009 (7)
February 2009 (5)
March 2009 (10)
April 2009 (2)
May 2009 (6)
June 2009 (3)
July 2009 (4)
August 2009 (6)
September 2009 (3)
October 2009 (9)
November 2009 (10)
December 2009 (1)
January 2010 (1)
February 2010 (3)
March 2010 (6)
April 2010 (2)
May 2010 (3)
June 2010 (4)
July 2010 (3)

Building an Event Handler and Custom Error Page in SharePoint 2010 

Tags:

What's in this post:

  • How to build a Event Handler in SharePoint 2010 from scratch
  • How to leverage a custom error page when canceling an event

 

Example leveraged: This walkthrough builds an event handler that executes when a site is being deleted in a site collection. When such as action occurs, the handler cancels the action and redirects the user to a custom error page where it informs them that sites shouldn't be deleted, but rather they should be archived.

 

STEP 1) Create a new Visual Studio 2010 project. Use the Empty Project template and specify the .NET 3.5 framework:

 

 

STEP 2) Type the URL to the Site Collection you want to deploy your project to as well as specify you want to deploy as a full-trust solution:

 

 

STEP 3) In the newly created solution, open the Feature and the feature designer will appear. Change the Scope to Site. This will make it so the event handler will be deployed across the entire site collection:

 

 

STEP 4) Add the event handler to the project. Right click the project and choose Add, New Item…

 

 

Select the Event Handler item template:

 

 

Select which event you want the handler to listen for (for this demo, we're capturing the Site Deleting event under Web Events):

 

 

STEP 5) Add code to the event handler. Open the code file, and enter your logic. For example:

 

properties.Cancel = true;

properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;

properties.RedirectUrl = "/_layouts/CustomEventErrorHandler/CustomErrorPage.aspx"

 

In this example, the Site Deletion is terminated and the user is redirected to a custom error page. This is accomplished by specifying the Status property and assigning it to the CancelWithRedirectUrl enumeration within SPEventReceiverStates. Other options include:

 

  • SPEventReceiverStatus.CancelNoError, cancel action but don't raise an error to the user
  • SPEventReceiverStatus.CancelWithError, cancel action and show default error dialog box (default)
  • SPEventReceiverStatus.CancelWithRedirectUrl, cancel error and redirect user to custom error page. Note that a RedirectUrl MUST be specified
  • SPEventReceiverStatus.Continue, do not cancel the action, even if properties.Cancel is set to true…

 

 

To Setup the Custom Error Page, Continue… (otherwise Deploy)

 

 

STEP 6) Add new application page by right clicking the project, and choose Add, New Item. Select Application Page:

 

 

Notice it creates a Layouts "Mapped Folder" that gets packaged up in the Feature/Solution…

 

STEP 7) Add page verbiage and other logic. In my example, I add some language that describes to end users that they need to archive sites (through some external process) rather than delete them:

 

 

STEP 8) Deploy and test the new Feature. Right click the project, and click Deploy:

 

 

Note – this will automatically activate the feature on the Site collection specified in Step 2.

 

Now, attempt to delete a site within that Site Collection:

 

 

 

SO EASY!

 

Phil

 
Posted by BENDER\pwicklund on 21-Oct-09
0  Comment  |  Trackback Url  | 0  Link to this post | Bookmark this post with:        
 
Failed to render control: Value does not fall within the expected range.

Comments

Bookmark and Share

Note: Facebook no longer sends notifications for comments, so it may be a number of days before I see your post. For urgent matters, click "Contact Me" on the top nav. More info: Click Here.