0

I am creating folder into sharepoint list and it successfully created.

But my list having unique permission(inheritance break from parent site) and only Group1 is having contribute permission on list.

My issue is when folder is created Group1 is not assigned permission on newly created Folder.

So I want to assign same group with same permission on newly created folder. I am using below code to create folder:

function CreateFolder(listName, relativePath, folderName) 
{
    // var deferred = $q.defer();
    var deferred = $.Deferred();
    var clientContext;
    var oWebsite;
    var oList;
    var itemCreateInfo;
    var siteUrl = _spPageContextInfo.webAbsoluteUrl;

    clientContext = new SP.ClientContext(siteUrl);//new SP.ClientContext.get_current();
    oWebsite = clientContext.get_web();
    oList = oWebsite.get_lists().getByTitle(listName);

    itemCreateInfo = new SP.ListItemCreationInformation();
    itemCreateInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);
    itemCreateInfo.set_leafName(relativePath);
    this.oListItem = oList.addItem(itemCreateInfo);
    this.oListItem.set_item('Title', folderName);
    this.oListItem.update();
    //TRied below code to assign permission but not worked Start
    var collGroup = clientContext.get_web().get_siteGroups();        
    var oGroup = collGroup.getById(223); 
    var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);    
    collRoleDefinitionBinding.add(clientContext.get_web().get_roleDefinitions().getByType(SP.RoleType.administrator)); 
    this.oListItem.get_roleAssignments().add(oGroup, collRoleDefinitionBinding);
    this.oListItem.update();
    //TRied below code to assign permission but not worked End
    clientContext.load(this.oListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, successHandler),
        Function.createDelegate(this, errorHandler)
    );

    function successHandler() {
        deferred.resolve('Folder is ready');
    }

    function errorHandler(sender, args) {
        if (args.get_errorCode() == -2130245363) {
            //Folder already exists
            deferred.resolve('folder is already exists');
        }
        else
            deferred.reject('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    return deferred.promise();

};
  • I think you need to call clientContext.executeQueryAsync() after you are trying to create the folder and then try to assign the permission to the new created folder. – Ankit Katiyar Jun 6 '18 at 7:23
  • @AnkitKatiyar, in this scenario he'll have new folder with unique permissions, right? – AlexZh Jun 6 '18 at 7:33
  • @AlexZh: Yes..if we are creating a new folder in document library by default it will inherit the permission from its parent. So we have to first break the inheritance and then add the permission – Ankit Katiyar Jun 6 '18 at 7:37
  • @AnkitKatiyar: so here is oddly problem that author has: new folder does not inherit permissions – AlexZh Jun 6 '18 at 8:13
  • @AlexZh: I dont know how it is even possible. If the document library has unique permissions and it is shared with User2.. so after creating a folder inside it will be shared with User2 automatically. Please provide more details on the issue if the explanation does not justifies your problem – Ankit Katiyar Jun 6 '18 at 8:39
0

If your list has already unique permissions and the same permission structure you want in your Folder also then you don't have to assign the permission to the folder explicitly. The folder after creation will automatically inherit the permission of it's parent element (In your case it is List)

So the below code is sufficient to create the Folder in the List to which Group 1 will have permission.

function CreateFolder(listName, relativePath, folderName) 
{
    // var deferred = $q.defer();
    var deferred = $.Deferred();
    var clientContext;
    var oWebsite;
    var oList;
    var itemCreateInfo;
    var siteUrl = _spPageContextInfo.webAbsoluteUrl;

    clientContext = new SP.ClientContext(siteUrl);//new SP.ClientContext.get_current();
    oWebsite = clientContext.get_web();
    oList = oWebsite.get_lists().getByTitle(listName);

    itemCreateInfo = new SP.ListItemCreationInformation();
    itemCreateInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);
    itemCreateInfo.set_leafName(relativePath);
    this.oListItem = oList.addItem(itemCreateInfo);
    this.oListItem.set_item('Title', folderName);
    this.oListItem.update();
    clientContext.load(this.oListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, successHandler),
        Function.createDelegate(this, errorHandler)
    );

    function successHandler() {
        deferred.resolve('Folder is ready');
    }

    function errorHandler(sender, args) {
        if (args.get_errorCode() == -2130245363) {
            //Folder already exists
            deferred.resolve('folder is already exists');
        }
        else
            deferred.reject('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    return deferred.promise();

};

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.