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“
2. Make it as a Farm solution, choose the event receiver properties as in the below screen.
3. On clicking the “Finish” button, Visual Studio will create the project structure as:
4. Now, in the Elements.xml file, change the ListTemplateID=”101″ (Which means all document Libraries) to ListURL=”RelativePathOfDocLibrary” say: ProjectDocuments.
Change the <Receivers attribute
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.
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.
Heloo! can i restrict the files also the .docs, and xls please guide me if it yes.. THANK YOU
Can we restrict the files which do not have extension in SP 2013? If yes, Can you please guide
how can this be done in 2007? that is what we have. thanks
You can do it in MOSS 2007 also. These two articles will help you: Create Event Handler in MOSS 2007 with WSP Builder
Associate Event Handler to particular list
I am getting following error message while using the same steps as above
Server Error in ‘/’ Application.
There is nothing complex in the code. To troubleshoot, reveal the actual error by changing web.config file. Get the Detailed Error and Disable Friendly Error Page in SharePoint
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.
You can apply this event receiver to a particular library by providing “Receiver ListUrl” in Elements.xml as in the above screen shot.
I’m getting the same error
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.
Referred this post to one of my friend… who asked about the same requirement.. 🙂