Sharepoint 2010 Event Handler Redirection to Custom Error page

Sharepoint 2010 comes with bundle of new features to support different business scenarios. In sharepoint 2007 most of the developer had a hard time in handling the following scenario.

Scenario

In a custom event receiver redirect user to a custom error page after the validation failure.

In sharepoint 2007 there was no support for this scenario so usually every one set the error message and gets redirected to default out of the box error page. This default page will not match the theme of the site as well as the user experience.

Sharepoint 2010 comes with new properties for handling the scenario

properties.Status = SPEventRecieverStatus.CancelWithRedirectUrl;
Properties.RedirectUrl = “/_layouts/MyBlog/CustomError.aspx"


public override void ItemAdding(SPItemEventProperties properties) {                 base.ItemAdding(properties);                 string tite = properties.AfterProperties[“title”].ToString();                 if(tite.Contains(“_”))                 {                                 properties.Cancel = true;                                 properties.Status = SPEventRecieverStatus.CancelWithRedirectUrl;                                 Properties.RedirectUrl = “/_layouts/MyBlog/CustomError.aspx?errormessage =Inavlid                                 Title”;                 } }


Create a application page called customerror.aspx and show the error message from the query string

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="Server">

   <asp:Label ID="lblError" runat="server" > </asp:Label>
</asp:Content>


Add the following code in the page load

protected void Page_Load(object sender EventArgs e) {       lblError.Text = Request.Param[“Error”]; }

Ebook Download
View all
Learn
View all