1

I want to break the inheritance to modify the permissions of current page in SharePoint using JavaScript(JSOM)

1 Answer 1

0

You can try the below SharePoint JSOM code snippet to break the current page or list item permission and assign the new permission to the group.

    function breakpermission(groupName) {

    var clientContext = new SP.ClientContext();
    oList = clientContext.get_web().get_lists().getByTitle(_spPageContextInfo.listTitle);
    oListItem = oList.getItemById(_spPageContextInfo.pageItemId);
    oListItem.breakRoleInheritance(false);

    //Bind Contribute permission to the Group
    oGroup = clientContext.get_web().get_siteGroups().getByName(groupName);
    var groupPermissionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);
    groupPermissionBinding.add(clientContext.get_web().get_roleDefinitions().getByType(SP.RoleType.contributor));

    //Assign binded permission to the page or listitem
    oListItem.get_roleAssignments().add(oGroup, collRoleDefinitionBinding);

    clientContext.load(oGroup);
    clientContext.load(oList);
    clientContext.load(oListItem);


    clientContext.executeQueryAsync(
        Function.createDelegate(this, function() {
            alert('Applied the unique permission and assigns contribute permission to the group');
        }),
        Function.createDelegate(this, function(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }));
}
function injectMethod() {
    breakpermission("Site Members");
}
ExecuteOrDelayUntilScriptLoaded(injectMethod, "sp.js");

I'll hope, the above code helps you.

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.