0

Is it possible to add a SharePoint Group to a library folder using JSOM in Office 365?

I'm using a script editor webpart on an aspx page to run the code. I managed to create SP Group, create folders, break inheritance of the folder but I'm unable to add the SP group to this folder.

Here is the code I'm trying. Function a() is where I would expect the SP Group to be added but I'm getting error that says Unknown Error undefined and its on the variable where I call myFolder.get_listItemAllFields().get_roleAssignments();

var groupCollection;    
var clientContext;
var currentWeb;
var folderUrl = '/sites/iss/Documents/ABC';
var newCustomerGroup;

function AddGroupToFolder(groupName)
{
    var myFolder;
    if (groupName.trim().length > 0) {
    GetCustomerFolderUrl();
}
else { alert('Group Name cannot be empty') }
}    

function GetCustomerFolderUrl() {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
    clientContext = new SP.ClientContext.get_current();
    currentWeb = clientContext.get_web();
    clientContext.load(currentWeb);        
    clientContext.executeQueryAsync(Function.createDelegate(this, this.GetCustomerFolderUrlSuccess), Function.createDelegate(this, this.GetCustomerFolderUrlFailed));
});
}

function GetCustomerFolderUrlSuccess() {
console.log('From: Inside GetCustomerFolderUrlSuccess');
myFolder = currentWeb.getFolderByServerRelativeUrl(folderUrl);
clientContext.load(myFolder);
clientContext.executeQueryAsync(Function.createDelegate(this, this.GetFolderPropertiesSuccess), Function.createDelegate(this, this.GetFolderPropertiesFailed));
}

function GetCustomerFolderUrlFailed(sender, args) {
alert('GetCustomerFolderUrlFailed failed. ' + args.get_message() + '\n' + args.get_stackTrace());
  }

function GetFolderPropertiesSuccess() {
console.log('CustPortal: From GetFolderPropertiesSuccess');    
//console.log('CustPortal: Folder is ' + myFolder.get_name());
GetCustGroupDetails(myFolder.get_name());
 }

function GetFolderPropertiesFailed(sender, args) {
alert('GetFolderPropertiesFailed failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

function GetCustGroupDetails(custFolder) {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
    clientContext = new SP.ClientContext.get_current();
    //currentWeb = clientContext.get_web();

    groupCollection = clientContext.get_web().get_siteGroups();        
    newCustomerGroup = groupCollection.getByName(custFolder);
    clientContext.load(newCustomerGroup);        
    clientContext.executeQueryAsync(Function.createDelegate(this, this.GetCustGroupDetailsSuccess), Function.createDelegate(this, this.GetCustGroupDetailsFailed));        
    });
}

function GetCustGroupDetailsSuccess() {
console.log('Group Name: ' + newCustomerGroup.get_title());
a();
}

function GetCustGroupDetailsFailed(sender, args) {
console.log('GetCustGroupDetailsFailed failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

function a(){
var rdContribute = currentWeb.get_roleDefinitions().getByName('Contribute');
var collContribute = SP.RoleDefinitionBindingCollection.newObject(clientContext);
collContribute.add(rdContribute);

var assignments = myFolder.get_listItemAllFields().get_roleAssignments();
//console.log('CustPortal: A ' + assignments.get_member());
var roleAssignmentContribute = assignments.add(myFolder, collContribute);
clientContext.load(myFolder);
clientContext.executeQueryAsync(Function.createDelegate(this, this.AddingGroupToFolderSuccess), Function.createDelegate(this, this.AddingGroupToFolderFailed));
}

function AddingGroupToFolderSuccess() {
console.log('SP Group should be added, please check!');
}

function AddingGroupToFolderFailed(sender, args) {
console.log('AddingGroupToFolderFailed failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

1 Answer 1

1

Try getting the listItemAllFields object for your Folder which will have the get_roleAssignments() method on it.

Then you can use the .get_roleAssignments().add() method to assign a user to the right role for that object like in this MSDN sample: "How to: Work with Roles Using JavaScript"

You'll need to load it first (with another call to load or with an Include statement on the folder object's load call) before it will have any properties if you're trying to inspect it.

7
  • I tried the following code but it seems the get_listItemAllFields().get_roleAssignments() is not returning any value.
    – Manu
    Dec 15, 2014 at 16:58
  • var rdContribute = currentWeb.get_roleDefinitions().getByName('Contribute'); var collContribute = SP.RoleDefinitionBindingCollection.newObject(clientContext); collContribute.add(rdContribute); var assignments = myFolder.get_listItemAllFields().get_roleAssignments(); var roleAssignmentContribute = assignments.add(myFolder, collContribute); clientContext.load(myFolder); clientContext.executeQueryAsync(Function.createDelegate(this, this.AddingGroupToFolderSuccess), Function.createDelegate(this, this.AddingGroupToFolderFailed));
    – Manu
    Dec 15, 2014 at 17:06
  • If you want to add more code just edit your original question with an update; I've looked at what you just posted though and the assignment is more complex than what you're attempting. You need to get the user you want to assign the permissions to, as well as which role definition you want to assign, then you use myFolder.get_listItemAllFields().get_roleAssignments().add() to assign the user and the role to the folder. Combine the info in my answer with this SO answer from Vadim: stackoverflow.com/questions/24897837/…
    – John-M
    Dec 15, 2014 at 17:38
  • Hi John, I've updated my post with full code I'm using. I'm trying to add a SP group to a folder not a user. I am using Vladim's code and helps in breaking inheritance of folder but I need to add group to folder.
    – Manu
    Dec 15, 2014 at 23:27
  • the first argument in assignments.add(myFolder, collContribute); shouldn't be the folder, it should be the group you're intending to add permissions for, you're already acting on the folder because assignments is myFolder.get_listItemAllFields().get_roleAssignments() -- msdn.microsoft.com/en-us/library/office/…
    – John-M
    Dec 15, 2014 at 23:52

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.