WebHosting

Showing posts with label setting permission splist item. Show all posts
Showing posts with label setting permission splist item. Show all posts

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();
}

Featured Posts

OBS Browser Source Not Working? How to Interact With Web Pages Inside OBS Studio

If you're using OBS Studio to display a website, YouTube page, dashboard, live poll, chat widget, or web application, you may notice so...