Custom Error Message for Event Receiver in SharePoint 2010

Mohemmad K picture Mohemmad K · Jul 24, 2013 · Viewed 7.5k times · Source

I want users to upload the .doc files only in the document library.

To do so, I have developed an event receiver in Visual Studio 2010.

My code is as follows:

public override void ItemAdding(SPItemEventProperties properties)
{
       try
       {

           base.ItemAdding(properties);
           EventFiringEnabled = false;

           if (!properties.AfterUrl.EndsWith("doc"))
           {
               properties.ErrorMessage = "You are allowed to updload only .doc files";
               properties.Status = SPEventReceiverStatus.CancelWithError;
               properties.Cancel = true;

           }
       }
       catch (Exception ex)
       {
           properties.Status = SPEventReceiverStatus.CancelWithError;
           properties.ErrorMessage = ex.Message.ToString();
           properties.Cancel = true;
       }

}

The code is referred from this example.

My problem is that while I am uploading non-doc files it is preventing but with the system error message not the user friendly as defined in properties.ErrorMessage.

How do I solve this?

Please help.

Answer

Amit Thakkar picture Amit Thakkar · Jul 24, 2013

I used the same code you have provided in your question, I get custom error message displayed as shown in below image -

enter image description here

Please provide details of the error you are getting.