site.RootWeb.RoleDefinitions.GetByType(SPRoleType.None) is raising the following error:- “Value does not fall within the expected range.”

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/205010

Question

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

Was it helpful?

Solution

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)

OTHER TIPS

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:

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top