How to Implement Custom Error Page Redirect in SharePoint 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 a 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, users tried uploading word files, receives the error message, and complained 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! Let’s get started! Do these 3 modifications to the existing project to get the SharePoint event receiver custom error page.

SharePoint redirect from event receiver:

1. Update the Event Receiver: ItemAdding Section’s Code would be:

<pre class="brush:c#">public override void ItemAdding(SPItemEventProperties properties)
{
   base.ItemAdding(properties);

   if (!properties.AfterUrl.EndsWith("pdf"))
   {
	   properties.Status=SPEventReceiverStatus.CancelWithRedirectUrl;
	   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>
<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>
</div>
</asp:Content>

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

sharepoint 2010 event receiver custom error

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

One thought on “How to Implement Custom Error Page Redirect in SharePoint Event Receivers

Leave a Reply

Your email address will not be published. Required fields are marked *