SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working with SharePoint 2010. I am creating groups in my site through code. There is no issue in that. At the time of creation I provide "Reader" role for my groups. But when I browse the listing of site groups in my site the group shows it has "Limited Access" role. Could any one help me on this?

 public static SPGroup CreateGroup(SPWeb web1, string groupName)
  SPWeb web = web1.Site.RootWeb;

  SPUser user = web.CurrentUser;
  web.SiteGroups.Add(groupName, user, user, groupName);
  SPGroup oGroup = web.SiteGroups.TryGet(groupName);
  if (oGroup != null)
    SPRoleDefinition role = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
    SPRoleAssignment roleAssignment = new SPRoleAssignment(oGroup);
    return oGroup;
    return null;
share|improve this question
Can you provide some code on how are you assigning the role? – Amadeo Gallardo Oct 30 '12 at 12:45
Please see my edits for the code I am using. – V.B Oct 31 '12 at 8:53

I don't see anything evidently wrong with that code.

Can you try "AllowingUnsafeUpdates" before doing the whole operation? Such as:

web.AllowUnsafeUpdates = true;

Also: were you able to debug into this piece of code? Does everything run as expected? Here are some things that you would want to check:

. The group does exist
. It finds the contributor role
. It's properly creating the RoleAssignment (instantiating it for the group, and adding the role)
. web.Update() succeeds (no exception thrown)

. At this point, try this: inspect the web object, get the group from SiteGroups and check if the RoleAssignment does exist at this stage.

Let's see what it comes out from the debugging session to further investigate.

share|improve this answer
I don't recommend using web.AllowUnsafeUpdates = true;. Most of time, it's not required. If you get exception, you should call SPUtility.ValidateFormDigest to validate the XSS token on POST methods. – Steve B Nov 14 '13 at 8:26
Also, check that you're running the PowerShell commands with Administrator permissions in the SharePoint Management Shell. – TempaC Nov 14 '13 at 13:48

This is the way I do it

  SPRoleAssignment roleAssignment = new SPRoleAssignment(visitorsGroupCreated);
                                SPRoleDefinitionBindingCollection roleDefBindings =roleAssignment.RoleDefinitionBindings;
                                SPRoleDefinition contributeRoleDefinition =web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                web.AllowUnsafeUpdates = false;

and you have to put web.allowunsafeupdates to true or it wont work.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.