2

I have to set approve permission using JavaScript client object model. I have used below code to set other permission. But for Approve it is not working.

function breakSecurityInheritance(lastItemId) {
    var siteUrl = _spPageContextInfo.webAbsoluteUrl;
    var clientPContext = new SP.ClientContext(siteUrl);
    var collGroup = clientPContext.get_web().get_siteGroups();
    var oList = clientPContext.get_web().get_lists().getByTitle($('[id$=hdnHomePagePreviewList]').val());
    this.oListPItem = oList.getItemById(lastItemId);

    // Authoring group permissions start
    oListPItem.breakRoleInheritance(false, true);

    var allGropIds = $('[id$=hdnAllAuthoringGroupIds]').val().split(';');
    for (var v = 0; v < allGropIds.length; v++) {
        var groupDetails = allGropIds[v].split(',');
       if (groupDetails[1] == "Contribute") {
            var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientPContext);
            collRoleDefinitionBinding.add(clientPContext.get_web().get_roleDefinitions().getByType(SP.RoleType.contributor));
        }
        else if (groupDetails[1] == "Full Control") {
            var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientPContext);
            collRoleDefinitionBinding.add(clientPContext.get_web().get_roleDefinitions().getByType(SP.RoleType.administrator));
        }
        else if (groupDetails[1] == "Read") {
            var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientPContext);
            collRoleDefinitionBinding.add(clientPContext.get_web().get_roleDefinitions().getByType(SP.RoleType.reader));
        }
        var oGroup = collGroup.getById(groupDetails[0]);
        oListPItem.get_roleAssignments().add(oGroup, collRoleDefinitionBinding);
    }
    // Authoring group permissions end

    clientPContext.load(oListPItem);

    clientPContext.executeQueryAsync(Function.createDelegate(this, this.onQueryPermissionsSucceeded), Function.createDelegate(this, this.onQueryPermissionsFailed));
}

As SP.RoleType definition does not have property of Approve. Any idea what to do for approve permission?

From server side code I have written below code for approve permission and it is working.

 SPGroup group = web.SiteGroups[groupName];
 SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
 SPRoleDefinition roleDefinition = web.RoleDefinitions["Approve"]; 
 roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
 roles.Add(roleAssignment);

But how to do using JavaScript client object model?

1

There is "getByName" method on the client side that you can exploit. So add the following block to your code:

else if (groupDetails[1] == "Approve") {
        var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientPContext);
        collRoleDefinitionBinding.add(clientPContext.get_web().get_roleDefinitions().getByName('Approve'));
    }

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.