You cannot grant a user a permission level that is not attached to a web

If you receive this error message that looks like this:

Microsoft.SharePoint.SPException: You cannot grant a user a permission level that is not attached to a web.
  at Microsoft.SharePoint.SPRoleDefinitionBindingCollection.AddInternal(SPRoleDefinition roleDefinition)
  at Microsoft.SharePoint.SPRoleDefinitionBindingCollection.Add(SPRoleDefinition roleDefinition)

When you’re doing something like this:

private static SPRoleDefinition FindAddEditListItemsRoleDefinition(SPWeb site) {
    SPRoleDefinition definition = site.RoleDefinitions
        .FirstOrDefault(def => def.Name == ROLE_DEFINITION_TITLE);

    if (definition != null) return definition;

    definition = new SPRoleDefinition {
        Description = "Can view and edit list items only.",
        BasePermissions = SPBasePermissions.EditListItems
            | SPBasePermissions.ViewListItems
            | SPBasePermissions.ViewFormPages
            | SPBasePermissions.ViewPages

            | SPBasePermissions.Open
    site.RoleDefinitions.BreakInheritance(true, true);
    return definition;

It may be because SharePoint doesn’t allow you to use the SPRoleDefinition you just created and you have to return the one you just added. Stupid, but hopefully it will help someone somewhere.

    // you can't just return definition because SharePoint gives you
    //    "You cannot grant a user a permission level that is not
    //    attached to a web" when you attempt to use it, so
    //    you need to return it again
return site.RoleDefinitions
        .First(def => def.Name == ROLE_DEFINITION_TITLE);


Yannie said…
I’m looking for SQL Tutorial. Maybe you can help me find one (best one). I totally do not know how it works. But I badly needed to know about it. Maybe you can help me. Thanks!
Aroh Shukla said…
dude!!! Thanks a lot for ur blog post.. your post helped me a lot!!
Anonymous said…
After banging my head for an hour, I found your blog. Thanks!!!

God and Sharepoint work in mysterious ways. We must not seek understanding of either. ;-)
Anonymous said…
why you call site.RoleDefinitions.BreakInheritance(true, true);

Anonymous said…
Thanks, you saved my day :)