Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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);
    roleAssignment.RoleDefinitionBindings.Add(role);
    web.RoleAssignments.Add(roleAssignment);
    web.Update(); 
    return oGroup;
  }
  else
    return null;
}
share|improve this question
3  
Can you provide some code on how are you assigning the role? –  Amadeo Gallardo Oct 30 '12 at 12:45
1  
Please see my edits for the code I am using. –  V.B Oct 31 '12 at 8:53
add comment

2 Answers

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
add comment

This is the way I do it

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

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

share|improve this answer
add comment

Your Answer

 
discard

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.