Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 7025

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 2162

Deprecated: Hook imagify_allow_picture_tags_for_webp is deprecated since version 2.2! Use imagify_allow_picture_tags_for_nextgen instead. in /www/collab365_296/public/wp-includes/functions.php on line 5758
Change SharePoint group permissions from code - Collab365
Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Warning: Undefined array key "url" in /www/collab365_296/public/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/smart-site/classes/class-tvd-smart-shortcodes.php on line 85

2014-10-07

Just “quick and dirty”

public static void ChangePermissions(SPSecurableObject subject, SPPrincipal principal, SPRoleType roleType)
{
    SPRoleDefinition roleDefinition = null;
    SPRoleAssignment roleAssignment = subject.RoleAssignments.GetAssignmentByPrincipal(principal);

    if (roleAssignment != null && subject != null)
    {
        switch (subject.GetType().Name)
        {
            case "SPList":
                if (!((SPList)subject).HasUniqueRoleAssignments)
                    ((SPList)subject).BreakRoleInheritance(true,false);
                roleDefinition = ((SPList)subject).ParentWeb.RoleDefinitions.GetByType(roleType);
                break;
            case "SPWeb":
                if (!((SPWeb)subject).HasUniqueRoleAssignments)
                    ((SPWeb)subject).BreakRoleInheritance(true, false);
                roleDefinition = ((SPWeb)subject).RoleDefinitions.GetByType(roleType);
                break;
            case "SPItem":
                if (!((SPItem)subject).HasUniqueRoleAssignments)
                    ((SPItem)subject).BreakRoleInheritance(true, false);
                roleDefinition = ((SPItem)subject).Fields.List.ParentWeb.RoleDefinitions.GetByType(roleType);
                break;
            case "SPListItem":
                if (!((SPListItem)subject).HasUniqueRoleAssignments)
                    ((SPListItem)subject).BreakRoleInheritance(true, false);
                roleDefinition = ((SPListItem)subject).Fields.List.ParentWeb.RoleDefinitions.GetByType(roleType);
                break;
            default:
                break;
        }

        if (roleDefinition != null)
        {
            roleAssignment.RoleDefinitionBindings.RemoveAll();
            roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
            roleAssignment.Update();
        }
    }
}

Implementation:

static void Main(string[] args)
{
    using(SPSite site = new SPSite("http://devsp"))
    {
        using(SPWeb web = site.RootWeb)
        {
            SPList list = web.Lists.TryGetList("TestList");
            SPListItem item = list.Items[0]; //grab first item

            string groupName = "MyGroup";
            ChangePermissions((SPSecurableObject)web, web.SiteGroups.GetByName("groupName"), SPRoleType.Contributor);
            ChangePermissions((SPSecurableObject)list, web.SiteGroups.GetByName("groupName"), SPRoleType.Contributor);
            ChangePermissions((SPSecurableObject)item, web.SiteGroups.GetByName("groupName"), SPRoleType.Contributor);
        }
    }
}

 

Originally posted on my blog

(Visited 17 times, 1 visits today)

About the author 

PauliusKe

Summit Bundle

Get 200+ hours of Microsoft 365 Training for 27$!

Master Office 365, Power Platform & SharePoint & Teams With 200+ Hours Of Training Videos and 108 Ebooks in the Collab365 Academy. This offer is insane and is only available for a limited period.