Search This Blog

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

Discover powerful tools for digital investigations, IP intelligence, threat hunting, exposed services, data breach research and satellite imagery.

14 Powerful OSINT Tools for Cybersecurity | BytesXplorer </> Bytes Xplorer TECHNOLOGY • ...