8

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?

2 Answers 2

5

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

2
  • 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, 2011 at 16:50
  • see my updated snippets. ALso do not call base.ItemAdding after you set the error message, status and cancel property. Dec 2, 2011 at 17:46
1
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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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