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());
}

0 commentaires:
Enregistrer un commentaire