0

I am trying to add permission to the list which I have created in my app. I am trying to give current user some permission to add update delete items from my list. I have used one article from MSDN site. And my code is as bellow:

function execOperation() {
    debugger;
    context = new SP.ClientContext(appweburl);
    var factory =
        new SP.ProxyWebRequestExecutorFactory(
            appweburl
        );
    context.set_webRequestExecutorFactory(factory);
    appContextSite = new SP.AppContextSite(context, appweburl);
    var appContextSite2 = new SP.AppContextSite(context, hostweburl);

    var siteColl = appContextSite2.get_site();
    hostweb = appContextSite2.get_web();
    context.load(hostweb);

    web = appContextSite.get_web();
    context.load(web);
    user = web.get_currentUser();
    context.load(user);
    que_list = web.get_lists().getByTitle("MyList1");
    context.load(que_list);
    que_list.breakRoleInheritance(true, true);

    var permissions = new SP.BasePermissions();
    permissions.set(SP.PermissionKind.viewListItems);
    permissions.set(SP.PermissionKind.addListItems);
    permissions.set(SP.PermissionKind.editListItems);
    permissions.set(SP.PermissionKind.deleteListItems);

    // Create a new role definition.
    var roleDefinitionCreationInfo = new SP.RoleDefinitionCreationInformation();
    roleDefinitionCreationInfo.set_name('Manage List Items');
    roleDefinitionCreationInfo.set_description('Allows a user to manage list items');
    roleDefinitionCreationInfo.set_basePermissions(permissions);
    var roleDefinition = web.get_roleDefinitions().add(roleDefinitionCreationInfo);

    web.breakRoleInheritance(true, false);
    // Create a new RoleDefinitionBindingCollection.
    var newBindings = SP.RoleDefinitionBindingCollection.newObject(context);
    // Add the role to the collection.
    newBindings.add(roleDefinition);

    // Get the RoleAssignmentCollection for the target list.
    var assignments = que_list.get_roleAssignments();
    que_list.breakRoleInheritance(true, true);
    // Add the user to the target list and assign the use to the new RoleDefinitionBindingCollection.
    var roleAssignment = assignments.add(web.get_currentUser(), newBindings);

    context.executeQueryAsync(onSuccess, onFail);
}

I have just copied the code from that article. But when I run my app it give me error :

You cannot customize permission levels in a web site with inherited permission levels

Any suggestions, What am I doing wrong?
Any help will be appreciated...! Thank you in advance...!

0

I think the problem is in the breakRoleInheritance method,

Replace the below code

web.breakRoleInheritance(true, false);

With

 web.breakRoleInheritance(true, true);

Note:

SPWeb.BreakRoleInheritance method (Boolean, Boolean)

Parameters,

copyRoleAssignments - true to specify role assignments are copied from the parent website to this website; false to specify only the current user is assigned to security roles.

clearSubscopes - true to specify unique role assignments are cleared from child objects and then inherit role assignments from this website; false to specify unique role assignments defined on child objects are left in place.

Reference

|improve this answer|||||
  • I tried but getting the same error... :( – Rahul Gokani Feb 7 '14 at 11:18
-1
web.RoleDefinitions.BreakInheritance(true, true);
web.Update();

if you target the roledefinitions instead of the spweb this will get rid the error message

|improve this answer|||||

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.