Posted by: R Manimaran | October 11, 2010

Programatically Adding and Removing Permission to a Group in SharePoint List

Programmatically Adding and Removing Permission to a Group in SharePoint List

I have a SharePoint list which has the access to following groups as shown.

Here in the Readers group the permission level is set tp both Contribute and Read. I need to remove the contribute permission.

To do this using Object Model. Here is the generic method to remove a Permission.

Note: If a Group has only one Permission already and if you remove that permission, then that group get deleted from that list. For ex. Here we have a group names Approvers with a Permission Design. If I use the below function to remove that group then it will get deleted from that list as shown. So do the Add operation first and followed by delete operation.

Remove Permission:

public static void RemovePermission(SPList list, string groupName, string permissionName)

{

SPPrincipal userGroup = FindUserOrSiteGroup(list.ParentWeb.Site, groupName);

SPRoleAssignment spRoleAssign = list.RoleAssignments.GetAssignmentByPrincipal(userGroup);

SPRoleDefinition role = list.ParentWeb.RoleDefinitions[permissionName];

spRoleAssign.RoleDefinitionBindings.Add(role);

spRoleAssign.Update();

list.Update();

}

Here is the procedure to add a Permission to the Group attached to a List.

Add Permission:

public static void AddPermission(SPList list, string groupName, string permissionName)

{

SPSecurity.RunWithElevatedPrivileges(delegate()

{

SPPrincipal userGroup = FindUserOrSiteGroup(site, groupName);

SPRoleAssignment spRoleAssign = list.RoleAssignments.GetAssignmentByPrincipal(userGroup);

SPRoleDefinition role = web.RoleDefinitions[permissionName];

spRoleAssign.RoleDefinitionBindings.Add(role);

spRoleAssign.Update();

list.Update();

});

}

Utility Method:

private static SPPrincipal FindUserOrSiteGroup(SPSite site, string userOrGroup)

{

SPPrincipal myUser = null;

if (SPUtility.IsLoginValid(site, userOrGroup))

{

myUser = site.RootWeb.EnsureUser(userOrGroup);

}

else

{

//might be a group

foreach (SPGroup g in site.RootWeb.SiteGroups)

{

if (g.Name.ToUpper(CultureInfo.InvariantCulture) == userOrGroup.ToUpper(CultureInfo.InvariantCulture))

{

myUser = g;

break;

}

}

}

return myUser;

}

About these ads

Responses

  1. Hi, thanks for putting me on the right track. Seems though that you need to break the roleinheritance in the list, and also there seems to be a typo in the remove method.

    spRoleAssign.RoleDefinitionBindings.Add(role); -> spRoleAssign.RoleDefinitionBindings.Remove(role);

    Anyways, thanks for a great post.

  2. Exactly what I needed! Thanks for a great post!

  3. This piece of writing will assist the internet users for setting
    up new weblog or even a blog from start to end.

  4. whoah this weblog is wonderful i love studying
    your articles. Keep up the good work! You understand,
    a lot of people are hunting round for this information,
    you can aid them greatly.

  5. This website was… how do you say it? Relevant!! Finally I’ve found something which helped me. Thanks a lot!

  6. I just get Free psn code thats works perfect , so
    I give this website a big VOUCH.

  7. Hi to all, how is everything, I think every one is getting more from
    this site, and your views are nice in favor of new visitors.

  8. Its like you read my mind! You seem to know so much about
    this, like you wrote the book in it or something.

    I think that you can do with some pics to drive the message home a little bit, but other than that, this is magnificent blog.
    A great read. I’ll definitely be back.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.

Join 25 other followers

%d bloggers like this: