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)) try
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);
{
using (SPWeb _MyWeb = _MySite.OpenWeb())
{
{
_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();
}
}
SPGroupCollection spc = _MyWeb.SiteGroups;
SPUserCollection _spUSer = _MyWeb.AllUsers;
folder.Item.BreakRoleInheritance(true);//Break the inheritance of the created folder in the Document Library
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

Merul TechPosted Jun 18, 2012, 7:44 AM
One question, So how can I make the permission levels of current folder and all its subfolders equal to the permission levels of a newly copied folder? i.e. I have created a function to copy a folder, now I need to copy all its permission levels (SP2010) tnks in advanve, Marka
A RanjanPosted Mar 12, 2011, 6:32 AM
I have a question, in the line if(!ostrCurrentUserName.Equals(strCurrentUserName)), where is this strCurrentUserName defined. Also, it does not removethe AD group. Is there any way to remove the AD group?