Restrict File Types in SharePoint Document Library Upload using Event Receiver

Requirement: In a Project collaboration SharePoint site, had a requirement to allow users to upload only PDF files to a Document Library and prevent all other files from being uploaded.

Solution: Our requirement is basically to prevent files from being uploaded other than PDF, isn’t it? So let’s jump to visual studio and create an Event Receiver in SharePoint to restrict file types in the SharePoint document library.

How to Create an Event Receiver in SharePoint 2010?

Compared with MOSS 2007, Creating Event receivers in SharePoint 2010 is very easy With Visual Studio 2010. Here are the steps:

1. Create a New Visual Studio Project, Choose SharePoint >> 2010 >> Event Receiver >> Give it a Name. Say “Crescent.ProjectSites.AllowPDFOnly
sharepoint document library restrict file types on upload
2. Make it as a Farm solution, choose the event receiver properties as in the below screen.
sharepoint restrict file types for document library
3. On clicking the “Finish” button, Visual Studio will create the project structure as:
sharepoint 2010 document library restrict file types
4. Now, in the Elements.xml file, change the ListTemplateID=”101″ (Which means all document Libraries) to ListURL=”RelativePathOfDocLibrary” say: ProjectDocuments.
sharepoint document library restrict file type
Change the <Receivers attribute restrict file types in document library sharepoint
5. Update the Event receiver ItemAdding section’s code as below:

if (!properties.AfterUrl.EndsWith("pdf"))
{
   properties.ErrorMessage = "You are allowed to upload only PDF Files!";
   properties.Status = SPEventReceiverStatus.CancelWithError;
   properties.Cancel = true;
}

So, The complete code would be:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace Crescent.ProjectSites.AllowPDFOnly.EventReceiver1
{
    public class EventReceiver1 : SPItemEventReceiver
    {
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);

           if (!properties.AfterUrl.EndsWith("pdf"))
           {
               properties.ErrorMessage = "You are allowed to upload only PDF Files!";
               properties.Status = SPEventReceiverStatus.CancelWithError;
               properties.Cancel = true;
           }
       }
    }
}

See the result in action! SharePoint document library restricts file type other than PDF.

sharepoint document library restrict file type

Drawback: This will prevent creating sub-folders which doesn’t end with PDF! so if you need a folder, name it “folder.pdf” and then rename it!

File Controller Feature from kwizcom allows you to achieve the same! You can restrict files on upload based on File Type, size.

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

11 thoughts on “Restrict File Types in SharePoint Document Library Upload using Event Receiver

  • February 27, 2020 at 7:12 AM

    Heloo! can i restrict the files also the .docs, and xls please guide me if it yes.. THANK YOU

    Reply
  • December 5, 2017 at 3:06 PM

    Can we restrict the files which do not have extension in SP 2013? If yes, Can you please guide

    Reply
  • January 22, 2013 at 7:42 PM

    how can this be done in 2007? that is what we have. thanks

    Reply
  • December 11, 2012 at 9:03 AM

    I am getting following error message while using the same steps as above
    Server Error in ‘/’ Application.

    Reply
  • December 10, 2012 at 8:28 PM

    Does this apply to all document libraries or just one in particular? There is only one library that I want to apply the .pdf restriction to.

    Reply
    • December 11, 2012 at 4:57 AM

      You can apply this event receiver to a particular library by providing “Receiver ListUrl” in Elements.xml as in the above screen shot.

      Reply
  • October 11, 2012 at 1:55 PM

    I’m getting the same error

    Reply
  • August 11, 2012 at 12:44 PM

    I am getting following error message while using the same steps as above
    Server Error in ‘/’ Application.
    ——————————————————————————–

    Runtime Error
    Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.

    Details: To enable the details of this specific error message to be viewable on the local server machine, please create a tag within a “web.config” configuration file located in the root directory of the current web application. This tag should then have its “mode” attribute set to “RemoteOnly”. To enable the details to be viewable on remote machines, please set “mode” to “Off”.

    Notes: The current error page you are seeing can be replaced by a custom error page by modifying the “defaultRedirect” attribute of the application’s configuration tag to point to a custom error page URL.

    Reply
  • July 20, 2012 at 4:41 PM

    Referred this post to one of my friend… who asked about the same requirement.. 🙂

    Reply

Leave a Reply