1

i have the following code , where i am trying to create a new user Group inside my RootSite, and do not assign any permission for it (as i am going to use it inside some sub-sites which have unique permissions). so i tried the following:-

SPRoleDefinition roleDefinition = site.RootWeb.RoleDefinitions.GetByType(SPRoleType.None);
SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
site.RootWeb.RoleAssignments.Add(roleAssignment);
site.RootWeb.Update();

but i got this exception:-

"Value does not fall within the expected range."

on the following line of code:-

SPRoleDefinition roleDefinition = site.RootWeb.RoleDefinitions.GetByType(SPRoleType.None);

So can anyone advice on this please? as when i create a new Group using the UI i can set a group without any permission levels..

1

In order to just create a new group, you don't need to use the SPRoleDefinition object.

You just need to add the group to the site group like this:

site.RootWeb.SiteGroups.Add("GroupName", site.RootWeb.SiteAdministrators[0], site.RootWeb.SiteAdministrators[0], "Your Group Description");

(insted of site.RootWeb.SiteAdministrators[0] you can use any other user)

  • so you mean in this case the group will be added to the RootWeb without any permission is this correct? and later on i can assign this Group to a sub-site (which have unique permission) and assign it a permission level (such as Contribute)??? – john Gu Jan 14 '17 at 2:34
  • can you adivce on my above comment please? and thanks for the useful reply.. – john Gu Jan 15 '17 at 1:25
  • 1
    Yes, the group will be added to the RootWeb without any permission, and yes, you can assign later contribute permission on a sub-site – Cecilia Jan 15 '17 at 16:47
0

There was bug you cannot get RoleDefinition by Type for None. To workaround it I used this piece of code:

    if (roleDefinition.Type != SPRoleType.None)
{
     definition = web.RoleDefinitions.GetByType(roleDefinition.Type);
     roleAssignment.RoleDefinitionBindings.Add(definition);
}
else
{
     definition = web.RoleDefinitions.GetById(roleDefinition.Id);
     roleAssignment.RoleDefinitionBindings.Add(definition);
}

Ref:

  • so how i should define the "roleDefinition" ?? something as follow:- "SPRoleDefinition roleDefinition = new SPRoleDefinition();" – john Gu Jan 13 '17 at 15:29
0

As per the following blog and following the post. they having the issue when SPROLETYPE.NONE and they fix it using this code. RoleDefinition not worked with the type rather it is fine using the RoleDefiniation.ID

if (roleDefinition.Type != SPRoleType.None)
{
 definition = web.RoleDefinitions.GetByType(roleDefinition.Type);
 roleAssignment.RoleDefinitionBindings.Add(definition);
}
else
{
 definition = web.RoleDefinitions.GetById(roleDefinition.Id);
 roleAssignment.RoleDefinitionBindings.Add(definition);
}
  • but can you advice how i need to create the roleDefinition? for example as follow :- "SPRoleDefinition roleDefinition = new SPRoleDefinition();"? – john Gu Jan 13 '17 at 15:30
  • have you tried this one SPRoleDefinition roleDefinition = site.RootWeb.RoleDefinitions.GetById(roleDefinition.Id)? – Waqas Sarwar MVP Jan 13 '17 at 15:46
  • where i need to place this ? instead of "SPRoleDefinition roleDefinition = site.RootWeb.RoleDefinitions.GetByType(SPRoleType.None);"? – john Gu Jan 13 '17 at 15:52
  • also i am not sure how you want me to reference roleDefinition inside a new roleDefinition !! – john Gu Jan 13 '17 at 15:53
  • i am not developer but i refer the post which explain it as bug, i just give u an idea how can you create new sprole definition....i copy it from your codeline – Waqas Sarwar MVP Jan 13 '17 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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