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

Here is my scenario.

I would like to allow the only certain file types (doc,docx,pdf,ppt,pptx) into my document library. I wrote an ItemAdding event receiver for a document library as follows. But I am unable to prevent the uploading. Can someone guide me?

{
    checking the file type here.
}

properties.Cancel = true;
//properties.Status = SPEventReceiverStatus.CancelWithError;
properties.ErrorMessage = "File extension not allowed here";
// properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
SPUtility.TransferToErrorPage("Invalid File Type");

What am I doing wrong here?

share|improve this question

2 Answers 2

You have to do following:

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

Or

properties.ErrorMessage = "File extension not allowed here";    
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.Cancel = true;

This is new to SharePoint 2010. More information here

EDIT: You can also block files of certain types at Web Application Level (without custom code), if that works for you: http://technet.microsoft.com/en-us/library/cc262496.aspx

share|improve this answer
    
Sorry it didn't work. I tried as you said. but no luck. ItemAdding event receiver for document library is behaving differently –  Share Dec 2 '11 at 16:50
    
see my updated snippets. ALso do not call base.ItemAdding after you set the error message, status and cancel property. –  Ashish Patel Dec 2 '11 at 17:46
if(allowed file types checking) // your file type checking
{
    //allowed file types
}
else
{
    properties.Cancel = true;
    properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
    properties.RedirectUrl = "/_layouts/error_Custom.aspx?Error=" + "Your error Message";
    //if you create any custom error page 
    this.EventFiringEnabled = false;
}
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.