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?

Phil Greer
  • 2,287
  • 9
  • 21
  • 23
Share
  • 483
  • 6
  • 17

2 Answers2

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

Ashish Patel
  • 11,385
  • 3
  • 22
  • 29
  • Sorry it didn't work. I tried as you said. but no luck. ItemAdding event receiver for document library is behaving differently – Share Dec 02 '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 02 '11 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;
}
Stu Pegg
  • 4,623
  • 7
  • 48
  • 91
Prashob J
  • 11
  • 1