SIGN UP MEMBER LOGIN:    
ARTICLE

How to redirect to a Custom Page for Event Receiver in SharePoint 2010

Posted by Vijai Anand Articles | SharePoint November 17, 2010
This article shows how to redirect to a Custom Page for Event Receiver in SharePoint 2010
Reader Level:

Introduction:

An event receiver in SharePoint allows great flexibility to meet various user requirements, and with each version of SharePoint there are improvements to enhance their usability. One of these added abilities is to redirect a user to a custom page while cancelling the event from an event receiver. Let's consider this scenario: We need to allow users to only upload documents into a document library from 9 a.m. to 5 p.m. As this is a new policy, there should be a page that displays the details of the new policy, whenever a user upload is cancelled.

An "Item Adding" event receiver will take care of the first part of the requirement, but for the second part there is no Out-of-Box support to achieve this in SharePoint 2007. However, SharePoint 2010 provides an excellent way to do this, with little effort.

Steps Involved:
  1. Open Visual Studio 2010.
  2. Go to File => New => Project.
  3. Select Event Receiver under installed template category SharePoint and name it as EventReceiver.
  4. Target the .Net Framework 3.5.
  5. Click OK.
  6. In the SharePoint Customization Wizard, enter the URL of the local site where you want to use (http://demo2010a:5000/) and select Deploy as a farm solution. 
  7. Click Next. SharePoint Customization Wizard will pop up as shown below.

    1.gif

  8. Click Finish.
  9. Right click the Solution Explorer, click Add and select SharePoint "Layouts" Mapped Folder.

    2.gif

  10. Right Click on EventReceiver folder, click Add and select New Item.
  11. Select Application Page under installed template category SharePoint and name it As CustomError.aspx.

    3.gif

  12. In the EventReciever1.cs replace the method ItemAdding with the below one.

    public override void ItemAdding(SPItemEventProperties properties)

    {

        if (DateTime.Now.Hour > 9 || DateTime.Now.Hour < 17)

        {

            properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;

            properties.RedirectUrl = "/_layouts/EventReceiver/CustomError.aspx";

        }

    }

  13. CustomError.aspx

    <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>

    <%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>

    <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

    <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

    <%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>

    <%@ Import Namespace="Microsoft.SharePoint" %>

    <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomError.aspx.cs" Inherits="EventReceiver.Layouts.EventReceiver.CustomError" DynamicMasterPageFile="~masterurl/default.master" %>

     

    <asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

     

    </asp:Content>

     

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

    <label id="lblErrorMessage" runat="server"><b>You can upload documents only between 9am to 5pm </b></label>

    </asp:Content>

     

    <asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">

    Error Message:

    </asp:Content>

     

    <asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >

    My Application Page

    </asp:Content>

  14. Build the solution and deploy it.
  15. Try to Add the Document in the SharePoint Document Library after 5 p.m. or before 9 a.m. you will be redirected to the custom page as shown below.

    4.gif
Summary:

Thus the Custom page is created and when the event is cancelled it is redirected to the custom page.

Login to add your contents and source code to this article
share this article :
post comment
 

Thank u:-)

Posted by Vijai Anand Nov 18, 2010

good post for event receiver item adding event

Posted by srinivas thallapelli Nov 18, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor