programmatically setting permission to a subfolder in a document library in SharePoint ?
-
Wednesday, June 01, 2011 1:21 PMAny way to programmatically setting permission to a subfolder in a document library in SharePoint 2010 ?
All Replies
-
Wednesday, June 01, 2011 1:31 PM
Hi Amine,
Use following code to assign permissions on Folders,
public void GrantSiteGroupPermissionOnFolder(string SiteName, string webPath, string libraryName, Guid folderGuid, string orgName, string roleName, string SPpermissionLevelName)
{
using (SPSite site = new SPSite("YOUR-SITE-URL"))
{
using (SPWeb web = site.OpenWeb(webPath))
{
SPList list = web.Lists[libraryName];
SPFolder folder = list.Items[folderGuid].Folder;
GrantPrincipalPermissionOnSecurableObject(list, web.SiteGroups[GroupName(orgName, roleName)], SPpermissionLevelName, web.RoleDefinitions);
}
}
}
private void GrantPrincipalPermissionOnSecurableObject(SPSecurableObject so, SPPrincipal principal, string SPpermissionLevelName, SPRoleDefinitionCollection roleDefs)
{
if (so.RoleAssignments.Count == 0) // permissions are inherited
so.BreakRoleInheritance(true);
SPRoleAssignment roleAssignment = new SPRoleAssignment(principal);
SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
roleDefBindings.Add(roleDefs[SPpermissionLevelName]);
so.RoleAssignments.Add(roleAssignment);
}
Ashraf ul Islam
Sharepoint Consultant- Marked As Answer by Amine Sassi Wednesday, June 01, 2011 1:53 PM
-
Wednesday, June 01, 2011 1:33 PM
Yes, you can set permissions to sub folders programmatically in a document library. Go through the following link:
You might need to do a bit of tweaking to have it work with SharePoint 2010, but I believe the classes used are still present with the same names in 2010 object model.
Hope that helps!
Ahmed Tamjid Aijazi | SharePoint 2010 Consultant | Blog: www.tamjid.com | Twitter: @tamjidaijazi -
Monday, June 18, 2012 11:33 AM
Hi Ashraf,
So how can I make the permission levels of current folder equal to the permission levels of a newly copied folder?
thx in advance.
Marka Junior
-
Tuesday, June 26, 2012 9:52 AM
hi ashraf
how to generate link for share my private file,like microsoft sky drive?
i want to send a link with specify permission for sharepoint user or specific email.
- Edited by meri_64 Tuesday, June 26, 2012 9:53 AM
-
Friday, April 12, 2013 7:13 PM
Ashraf,
Should you be passing the "folder" instead of the "list" in the function call:
GrantPrincipalPermissionOnSecurableObject(list, web.SiteGroups[GroupName(orgName, roleName)], SPpermissionLevelName, web.RoleDefinitions);
So the function would then be:
GrantPrincipalPermissionOnSecurableObject(folder, web.SiteGroups[GroupName(orgName, roleName)], SPpermissionLevelName, web.RoleDefinitions);
Chetan Garude, M.S. Computer Science, Microsoft Certified Professional

