WebHosting

Wednesday, October 5, 2011

Programmatically Set List or Document Library Permissions in SharePoint

using (SPSite spSiteCollection = new SPSite(siteCollectionURL))
{
    using (SPWeb parentWeb = spSiteCollection.OpenWeb())
    {
            SPWeb spSite = parentWeb.Webs["SiteName"];
            // Set site permissions
            List<string> principals = new List<string>(new string[] { "My Intranet Owners", "My Management Team", "System Account" });
            setUniquePermission(spSite, "Commercial Documents", principals);
        }
    }
}






private void setUniquePermission(SPWeb spWebSite, string listName, List<string> roleNames)
{
    SPList spList = spWebSite.Lists[listName];
    spList.BreakRoleInheritance(true);
    // Remove any roles that are not in list
    for (int i = spList.RoleAssignments.Count - 1; i >= 0; i--)
    {
        if (roleNames.IndexOf(spList.RoleAssignments[i].Member.Name) == -1)
            spList.RoleAssignments.Remove(i);
    }           
    spList.Update();
}

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

How to Enable and Configure Remote Desktop (RDP) on Ubuntu 24.04.4 LTS

Remote Desktop Protocol (RDP) support in Ubuntu has become considerably easier with the GNOME Remote Desktop stack included in modern Ubuntu...