Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

Error is as this

How do I solve this?

Please help.

share|improve this question
    
Are you sure it is not the catch ErrorMessage that is showing? –  Robert Lindgren Jul 24 '13 at 8:55
    
Yes I am posting the image... @RobertLindgren –  users1100 Jul 24 '13 at 8:56
    
That error message looks like you are not successfully capturing the error at all –  Robert Lindgren Jul 24 '13 at 9:01
    
First I put outside the catch() statement after I got an example from the MSDN site and I preferred to do that.. –  users1100 Jul 24 '13 at 9:03
    
What to do for capturing error successfully? @RobertLindgren –  users1100 Jul 24 '13 at 9:13

2 Answers 2

You can use the property Status of SPItemEventProperties to redirect the user to a custom page:

properties.Cancel = true;
properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl; 
properties.RedirectUrl = "/_layouts/MyApp/CustomErrorPage.aspx?Error=" + errorMessage;

Of course, you have to provision the CustomErrorPage.aspx and write the logic for it. There are some problems with ItemDeleting event, but this should not affect you: http://sharesilver.wordpress.com/2011/07/24/sharepoint-bugs-1-item-deleting-event-receiver/

share|improve this answer

First do a check for if(properties.ListItem.File!=null) to determine if listitem has file associated with it.

 properties.RedirectUrl = properties.Web.ServerRelativeUrl + "/_layouts/errorpage.aspx";
 properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
 properties.Cancel = true;

http://msdn.microsoft.com/en-us/library/gg981880.aspx#UsingEventReceiversInSPFPart2_CancelingAndRedirecting

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.