Domanda

I'm trying to change the permissions of an SP Group in an SPWeb using JSOM. I can add a new role using a new RoleDefinitionBindingCollection oblect to the group. But a similar approach to remove a role definition fails with an error saying:

Unable to get property 'remove' of undefined or null reference

Here is the code I use:

var clientContext;
var currentWeb;
var groupCollection;
var pMGroup;

function CloseProjectSite(siteTitle)
{
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
    clientContext = new SP.ClientContext('http://site collection/myweb');
    currentWeb = clientContext.get_web();
    groupCollection = clientContext.get_web().get_siteGroups();
    pMGroup = groupCollection.getByName(siteTitle + ' Project Managers');

    //Get Role Definition by name
    var rdRead = currentWeb.get_roleDefinitions().getByName('Read');
    var rdContribute = currentWeb.get_roleDefinitions().getByName('Contribute');
    // Create a new RoleDefinitionBindingCollection.
    var collRead = SP.RoleDefinitionBindingCollection.newObject(clientContext);
    var collContribute = SP.RoleDefinitionBindingCollection.newObject(clientContext);
    // Add the role to the collection.
    collRead.add(rdRead);
    collContribute.add(rdContribute);

    // Get the RoleAssignmentCollection for the target web.
    var assignments = currentWeb.get_roleAssignments();

    // assign the group to the new RoleDefinitionBindingCollection.
    var roleAssignmentRead = assignments.add(pMGroup, collRead);        

    var roleAssignmentContribute = assignments.remove(pMGroup, collContribute);        

    clientContext.load(currentWeb);
    clientContext.load(pMGroup);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.CloseProjectSiteSuccess), Function.createDelegate(this, this.CloseProjectSiteFailed));
});
}

It seems the RoleDefinitionBindingCollection doesn't have a

remove

function. Has anybody come across this before?

È stato utile?

Soluzione

Try the below code. You should first get the Role Assignment if the group from the role assignment collection of the web.Then use importRoleDefinitionBindings method of the roleassignment object to add the Read role definition binding. This will remove the existing bindings and add the new binding.

var clientContext;
    var currentWeb;
    var groupCollection;
    var pMGroup;

function CloseProjectSite(siteTitle)
{
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
    clientContext = new SP.ClientContext('http://site collection/myweb');
    currentWeb = clientContext.get_web();

    //Get Role Definition by name
    var rdRead = currentWeb.get_roleDefinitions().getByName('Read');
    // Create a new RoleDefinitionBindingCollection.
    var collRead = SP.RoleDefinitionBindingCollection.newObject(clientContext);
    // Add the role to the collection.
    collRead.add(rdRead);
    // Get the RoleAssignmentCollection for the target web.
    var assignments = currentWeb.get_roleAssignments();
     //Get the role assignment for the group using Group  's membership id
    var groupRoleAssignment=assignments.getByPrincipalId(23);
    groupRoleAssignment.importRoleDefinitionBindings(collRead);
    groupRoleAssignment.update();
    clientContext.executeQueryAsync(Function.createDelegate(this, this.CloseProjectSiteSuccess), Function.createDelegate(this, this.CloseProjectSiteFailed));
});
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top