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

Kali Linux Remote Desktop: Access GNOME from Windows Using Native RDP

  Kali Linux + GNOME 50 + GNOME Remote Desktop + Windows Remote Desktop (MSTSC) Getting a full GNOME desktop remotely on Kali Linux can be ...