Redirect a Custom Page in SharePoint 2010

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: an HR department wants to place a new policy which requires all the users to only upload documents into a document library from 9 a.m. to 5 p.m. Moreover, 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. The following diagram shows the flow.

Now, an “Item Adding” event receiver will take care of the first part of the requirement, but for the second part there is no OTB support to achieve this in SharePoint 2007. However, SharePoint 2010 provides an excellent way to do this, with little effort. The following steps shows the detail of how to achieve this.

First, create a new Visual Studio 2010 Beta 2 Solution and expand “SharePoint 2010” from “Installed Template;” select project of type “Event Receiver.” Give desired names to the solution and project. My previous article covers the event receiver project creation and some required configuration in detail, if you need more help, please refer to it.

The “SharePoint Customization Wizard” will appear. In “type of Event Receiver” select “List Item Events.” Select “Document Library” from “event source” drop down. Also select the checkbox for “An item is being added” from the list of available events. See the figure below.

After the project is created, create a page that will be displayed when a document upload is declined. For this, add a layouts folder to the project. You can do it by right clicking on the project and select “SharePoint “Layouts Mapped Folder;” see the figure below.

This will create a layouts folder, actually a subfolder with the same name as your project. This layout folder basically corresponds to SharePoint 2010 “Layouts” folder inside the “14” hive.

Right click this subfolder inside the “Layouts” folder, and select “Add, New Item.” This will open a new dialog, select “Application Page,” and use “HRDocumentUplaodMessage.aspx” as a name.

Click “Add.” This will add this application page to the layouts subfolder. Add the simple texts as shown below into the mentioned tags. The basic idea is to keep it simple, but this page can be made as much detailed and complex as required.

In order to add the logic to cancel the document upload, and show the custom page, add the following code fragment into the “Item Added” event handler.

Replace “[SubfolderName]” with the actual name of the subfolder created inside “layouts” directory in the previous step by Visual Studio. This is by default set to the same name as that of project.

The code inside the “Item Added” event receiver, first checks to make sure that, cancellation is done only for desired time slot. As “DateTime.Hour” is in 24 hours format, it’s a very easy check. The value “17” in 24 hour format equates to 6 p.m.

Next, the property “Status” is used. Its actually of type SPEventRecieverStatus, which in this case sets to “CancelWithRedirectUrl. ” This will make sure to cancel the event and then redirect to a specific URL. Another property “RedirectUrl” is set to the actual URL , which is the URL of the page which will be displayed when event is cancelled.

Once all of this is done, press F5. This will build and deploy the solution.

In order to do a test, make sure that current time on the machine where SharePoint 2010 is installed is between 9am to 5pm, otherwise logic in the event handler to display custom page will be not executed due to the time interval check .Finally browse to portal, and open any document library and try to upload a document, the custom page should be displayed as shown below. Close the dialog page, and view the document library, notice that the document was not added to the document library.

This show how easy is to make a redirect to a custom page, by using a new enumeration value in SharePoint 2010 Object Model.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.