How To make SharePoint document library folder unique Permission Programmatically


In this article I am showing how to create a folder in a SharePoint document library, make unique permissions for the folder then add users to that folder programmatically. This will be useful when you deal with the following scenario. I have a document library inheriting permissions from the parent site (say 50 users). If I create a folder in the library it also adds 50 users. Then I created another folder and edited the permission and added about 50 users. Now the problem is that if I again create a folder then it should inherit 100 users in that; if I want to remove all those many users then that will be tedious. The following code will help with that.
 

How to Programmatically Create Folder in Document Library
 

The following code will create a folder in a SharePoint document library. You have to give a folder name and document library name as shown below.

 

using (SPSite _MySite = new SPSite(site))
        {
            using (SPWeb _MyWeb = _MySite.OpenWeb())
            {

                try
                {
                    _MySite.AllowUnsafeUpdates = true;
                    _MyWeb.AllowUnsafeUpdates = true;
    SPDocumentLibrary _MyDocLibrary = (SPDocumentLibrary)_MyWeb.Lists["My Documents"];//Name of the Document Library
  SPFolderCollection _MyFolders = _MyWeb.Folders;
                    if (_MyDocLibrary != null)
             {
   if (!_MyWeb.GetFolder("Your Web Application URL"+ txtFolder.Text).Exists)
                        {
  _MyFolders.Add(documentLibrary + txtFolder.Text +"/");//Create Folder inDocument Library
  _MyDocLibrary.Update();
           }
}

 

How to make Folder unique Permission
 

The following code will enable unique permissions for the folder then I am removing each user from the folder.

Breaking permission inheritance
 

 SPFolder folder = _MyWeb.GetFolder(documentLibrary + txtFolder.Text);
             SPGroupCollection spc = _MyWeb.SiteGroups;
             SPUserCollection _spUSer = _MyWeb.AllUsers;
      folder.Item.BreakRoleInheritance(true);//Break the inheritance of the created folder in the Document Library
 

 

The following code will remove all users from the folder. It won't remove the current user who is doing this task.
 

foreach (SPUser oUser in _spUSer)
                  {
  SPPrincipal principal = (SPPrincipal)oUser;
  SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)oUser);
        _MySite.AllowUnsafeUpdates = true;
      _MyWeb.AllowUnsafeUpdates = true;
         try
                     {
   string ostrCurrentUserName = oUser.LoginName.ToString().Substrin
(oUser.LoginName.ToString().IndexOf('\\') + 1);
                                    if(!ostrCurrentUserName.Equals(strCurrentUserName))
 
                                        folder.Item.RoleAssignments.Remove((SPPrincipal)oUser);
                                }
                                catch (Exception exx)
                                {
           lblMessage.Visible = true;
 
                                    lblMessage.Text = oUser + "Doesnt have permission to create
folder"
;
                                }
 

                            }
                            //Removing the  sharepoint groups from the folder
                            foreach (SPGroup oGroup in spc)
                            {
                                // Response.Write(SPEncode.HtmlEncode(oGroup.Name) + "<BR>");
                                SPRoleAssignment roleAssignment = newSPRoleAssignment((SPPrincipal)oGroup);

                                _MySite.AllowUnsafeUpdates = true;
                                _MyWeb.AllowUnsafeUpdates = true;
                                folder.Item.RoleAssignments.Remove((SPPrincipal)oGroup);

                            }

 

How to add users to Folder
 

The following code will add users to the folder.

 

SPUser CurrentUser = _MyWeb.CurrentUser;
    GrantUserPermission(folder, _MyWeb, SPRoleType.Contributor, CurrentUser);
private
 static void GrantUserPermission (SPFolder CurrentListItem, SPWeboSPWeb, SPRoleType SPRoleType, SPPrincipal SPPrincipal)

    {
     SPRoleDefinition oSPRoleDefinition = oSPWeb.RoleDefinitions.GetByType(SPRoleType);
   SPRoleAssignment oSPRoleAssignment = new SPRoleAssignment(SPPrincipal);
        oSPRoleAssignment.RoleDefinitionBindings.Add(oSPRoleDefinition);
        CurrentListItem.Item.RoleAssignments.Add(oSPRoleAssignment);
        CurrentListItem.Update();
    }