SharePoint 2010 Custom Error Page Redirect in Event Receivers

In my last post Allow only Certain File Types to be Uploaded in SharePoint Document Library using Event Receiver I developed an Event receiver to prevent files other than PDF from being uploaded to a document library. So when user tries to upload a file other than PDF they'll get an Error message in the window saying "You are allowed to upload only PDF files". No issues!

But in most of the cases, user tried uploading word files, receives the error message and complains they don't know how to make the word or other file types to PDF. So, to help out users, what we wanted to do is instead of just saying "You are allowed to upload only PDF files", what if we instruct users on "How to convert the word document to PDF" with some descriptive text? Sounds good! Lets get started! Do these 3 modifications to the existing project to get SharePoint event receiver custom error page.

SharePoint redirect from event receiver:
1. Update the Event Receiver: ItemAdding Section's Code would be:
public override void ItemAdding(SPItemEventProperties properties)

           if (!properties.AfterUrl.EndsWith("pdf"))
               properties.RedirectUrl = "/_Layouts/Crescent.ProjectSites.AllowPDFOnly/PDFOnlyError.aspx";

2. Add a Application Page to the Project, optionally a Icon to represent Error.
sharepoint event receiver custom error page
Now the Project will be looking like:
sharepoint 2010 event receiver custom error page

3. Update the Error page: Under the PlaceHolderMain Place some content to give descriptive Error message:

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<div style="float:left"> 
    <asp:Image ID="Image1" runat="server" ImageUrl="/_Layouts/Crescent.ProjectSites.AllowPDFOnly/Error.png" />
<div style="width:700px; height:300px; padding-left:10px;"> 
<h2>You are allowed to Upload only PDF files. <br /></h2> <h3>Here is How to Convert Word files to PDF</h3>
<P>1. Open the file you wish to save.</P>
<P>2. Click the Microsoft Word button in the top left-hand corner.</P>
<P>3. Go to Save As > PDF or XPS. If this option is not available to you, go here to download Microsoft's free PDF and XPS converter. Note that this will only work for Windows; if you have a Mac, use the method below.</P>
<P>4. Type in the desired file name and adjust settings as necessary. Choose Minimum Size if you wish to reduce the size and quality.</P>
<P>5. Hit Publish. This will convert your document and open it in Adobe Reader (if you have it installed)</P>

See the Result in action: SharePoint 2010 event receiver custom error page.
sharepoint 2010 event receiver custom error
SharePoint 2010 event receiver custom error

1 comment:

Please Login and comment to get your questions answered!

Powered by Blogger.