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..

3 Answers 3

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)

3
  • 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 John
    Commented Jan 14, 2017 at 2:34
  • can you adivce on my above comment please? and thanks for the useful reply..
    – John John
    Commented Jan 15, 2017 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
    Commented Jan 15, 2017 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:

1
  • so how i should define the "roleDefinition" ?? something as follow:- "SPRoleDefinition roleDefinition = new SPRoleDefinition();"
    – John John
    Commented Jan 13, 2017 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);
}
5
  • but can you advice how i need to create the roleDefinition? for example as follow :- "SPRoleDefinition roleDefinition = new SPRoleDefinition();"?
    – John John
    Commented Jan 13, 2017 at 15:30
  • have you tried this one SPRoleDefinition roleDefinition = site.RootWeb.RoleDefinitions.GetById(roleDefinition.Id)?
    – Waqas Sarwar MVP
    Commented Jan 13, 2017 at 15:46
  • where i need to place this ? instead of "SPRoleDefinition roleDefinition = site.RootWeb.RoleDefinitions.GetByType(SPRoleType.None);"?
    – John John
    Commented Jan 13, 2017 at 15:52
  • also i am not sure how you want me to reference roleDefinition inside a new roleDefinition !!
    – John John
    Commented Jan 13, 2017 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
    Commented Jan 13, 2017 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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