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

LM Studio is a desktop application designed for developing and experimenting with large language models (LLMs) directly on your computer.

    LM Studio Overview LM Studio is a desktop application designed for developing and experimenting with large language models (LLMs...