Search This Blog

Sunday, June 19, 2016

Restict File Type in Sharepoint Document Library Using Event Receiver.


Creating Event receivers in SharePoint 2013 is very easy With Visual Studio 2010. Here are the steps:

1. Create a New Visual Studio Project, Choose SharePoint >> 2013 >> Event Receiver >> Give it a Name. Say "EventReceiver1"



2. Make it as a Farm solution, choose the event receiver properties as in the below screen.






3. On clicking "Finish" button, Visual Studio will create the project structure as and Now, in the Elements.xml file, change the ListTemplateID="101" (Which means all document Libraries) to ListURL="RelativePathOfDocLibrary" say: "CV".

Change the <Receivers attribute



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 ERTest.EventReceiver1
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);
           if (!properties.AfterUrl.EndsWith("pdf"))
           {
            

                         // Redirect to custom Page in SharePoint in  (using Aplication Page)

                  properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
                  properties.RedirectUrl = "/_Layouts/Restrictionfile/CustomPage.aspx";
               
                
                // Going to Restion Page og Sharepoint 
                 // properties.ErrorMessage = "You are allowed to upload only PDF File!";
                 //properties.Status = SPEventReceiverStatus.CancelWithError;
                 // properties.Cancel = true;
                
     


              

           }

       }


    }
}

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



Application Page for Redirect

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomPage.aspx.cs" Inherits="Restrictionfile.Layouts.Restrictionfile.CustomPage" DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<label id="lblErrorMessage" runat="server"><b>You are allowed to upload only PDF File! </b></label>
</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
  e CDR  Restriction page
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
e CDR  Restriction page</asp:Content>



No comments:

Post a Comment