Prevent creating folder in SharePoint Document Library and List

Lately I got requirement to prevent users to create folder in SharePoint Document Library and List throughout site collection.
Then I did quick search in codeplex and found someone already did it

From there, I’ve added / updated items in the solution. Below is the list of steps that I’ve done:

  • 1. Create New SharePoint Empty project, called it “CustomDisableFolder” and deploy it as farm solution.
  • 2. Add New Item > Empty Element > called it “RemoveNewFolderRibbon”. Then populate Elements.xml to:
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <CustomAction Id="CustomIdentifier.Ribbon.Documents.New.NewFolder.Hide" Location="CommandUI.Ribbon" RegistrationType="ContentType" RegistrationId="0x">
        <CommandUIExtension>
          <CommandUIDefinitions>
            <CommandUIDefinition Location="Ribbon.Documents.New.NewFolder">
            </CommandUIDefinition>
          </CommandUIDefinitions>
        </CommandUIExtension>
      </CustomAction>
      <CustomAction Id="Remove.ListItem.NewFolder" Location="CommandUI.Ribbon">
        <CommandUIExtension>
          <CommandUIDefinitions>
            <CommandUIDefinition
              Location="Ribbon.ListItem.New.NewFolder" />
          </CommandUIDefinitions>
        </CommandUIExtension>
      </CustomAction>
    </Elements>
    
  • 3. Add New Item > User Control > called it “AdditionalPageHeadUserControl.ascx”. Populate the ascx file with:
    <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
    <%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ 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=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
    <%@ Import Namespace="Microsoft.SharePoint" %>
    <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AdditionalPageHeadUserControl.ascx.cs" Inherits="CustomWeltmanDisableFolder.ControlTemplates.CustomWeltmanDisableFolder.AdditionalPageHeadUserControl" %>
    <script type="text/javascript">
        //If there is NO JQuery loaded previously
        //window.onload = function () {
        //    var allinputs = document.getElementsByTagName("input");
        //    for (var i = 0; i < allinputs.length; i++) {
        //        if (allinputs[i].getAttribute('value') != null && (allinputs[i].getAttribute('value') == "RadEnableFoldersYes" || allinputs[i].getAttribute('value') == "RadEnableFoldersNo")) {
        //            allinputs[i].setAttribute("disabled", true);
        //        }
        //    }
        //};
     
        // Assume JQuery loaded in master page
        $(function () {
            // Disable New Folder checkbox settings in List / Library Settings
            $("input[id*='RadEnableFoldersYes']").attr("disabled", true);
            $("input[id*='RadEnableFoldersNo']").attr("disabled", true);
        });
    </script>
    
  • 4. Add New Item > Empty Element > called it “AdditionalPageHeadElement”. Populate the Elements.xml into:
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Control Id="AdditionalPageHead" Sequence="90" ControlSrc="/_ControlTemplates/15/CustomDisableFolder/AdditionalPageHeadUserControl.ascx"></Control>
    </Elements>
    
  • 5. Add New Item > Event Receiver > ListAdded Event Receiver > called it “ListEventReceiver”. Populate ListAdded event to:
    public override void ListAdded(SPListEventProperties properties)
    {
     base.ListAdded(properties);
     SPSecurity.RunWithElevatedPrivileges(delegate()
     {
      using (SPWeb currWeb = new SPSite(properties.SiteId).OpenWeb(properties.Web.ID))
      {
       SPList currList = currWeb.Lists.TryGetList(properties.ListTitle);
       if (currList != null && currList.AllowDeletion)
       {
        currList.EnableFolderCreation = false;
        currList.Update();
       }
      }
     });
    }
    
  • 6. Add New Item > Event Receiver > ItemAdding Event Receiver > called it “ItemEventReceiver”. Populate ItemAdding event to:
    public override void ItemAdding(SPItemEventProperties properties)
    {
     base.ItemAdding(properties);
     if (properties.List != null && properties.List.AllowDeletion && properties.AfterUrl.IndexOf("/New folder") > 0)
     {
      properties.ErrorMessage = "Folder is not allowed, use metadata instead to provide additional information to a file.";
      properties.Status = SPEventReceiverStatus.CancelWithError;
     }
    }
    
  • 7. Add FeatureActivated Event Receiver to the Feature and set the Feature Scope to Site.
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
     SPSecurity.RunWithElevatedPrivileges(delegate()
     {
      using (SPSite site = new SPSite(((SPSite)properties.Feature.Parent).ID))
      {
       foreach (SPWeb eachWeb in site.AllWebs)
       {
        foreach(SPList list in eachWeb.Lists)
        {
         if (list != null && list.AllowDeletion )
         {
          list.EnableFolderCreation = false;
          list.Update();
         }
        }
       }
      }
     });
    }
    
  • 8. The Solution Explorer will going to look like below.DisableFolder3

Results
List / Library New Folder settings set to No and User will not be able to updated it.
DisableFolder4

New Folder button in ribbon gone.
DisableFolder1
DisableFolder5

Advertisements

Tagged: , , , , , , , , , , , ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: