Thursday, July 19, 2012

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 being uploaded other than PDF, isn't it? So lets jump to visual studio and create a Event Receiver in SharePoint to restrict file types in SharePoint document library.

How to Create a 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 "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 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 folder, name it like "folder.pdf" and then rename!

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



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Five Challenges in SharePoint Security
...And How to Solve Them. Free White Paper
*Sponsored


Check out these SharePoint products:

9 comments :

  1. Referred this post to one of my friend... who asked about the same requirement.. :)

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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.

    ReplyDelete
    Replies
    1. You can apply this event receiver to a particular library by providing "Receiver ListUrl" in Elements.xml as in the above screen shot.

      Delete
  4. I am getting following error message while using the same steps as above
    Server Error in '/' Application.

    ReplyDelete
    Replies
    1. There is nothing complex in the code. To troubleshoot, Reveal the actual error by changing web.config file. Get the Detailed Error and Disable Friedly Error Page in SharePoint

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

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...