none
programmatically setting permission to a subfolder in a document library in SharePoint ?

    Question

  • Any way to programmatically setting permission to a subfolder in a document library in SharePoint 2010 ?
    Wednesday, June 01, 2011 1:21 PM

Answers

  • 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:31 PM

All replies

  • 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:31 PM
  • Yes, you can set permissions to sub folders programmatically in a document library. Go through the following link:

    http://blogs.msdn.com/b/robgruen/archive/2007/11/15/how-to-programmatically-set-permissions-on-files-folders-in-a-sharepoint-document-library.aspx

    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
    Wednesday, June 01, 2011 1:33 PM
  • 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

    Monday, June 18, 2012 11:33 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
    Tuesday, June 26, 2012 9:52 AM
  • 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

    Friday, April 12, 2013 7:13 PM