3

Is it possible to use JSOM to assign unique folder permissions?

I found the SP.BasePermissions.set() method but can't seem to find much documentation on it. Much more out there using C# but unfortunately I'm limited to using JavaScript.

EDIT: Sorry if this was unclear - In addition to breaking the role inheritance I also need to assign permissions to the folder programmatically.

  • A couple things would need to happen when a folder is created: 1. Break parent inheritance 2. Grant current user permissions 3. Remove all other permissions – Bread Oct 14 '14 at 16:32
  • check this one community.spiceworks.com/topic/… – Waqas Sarwar MVP Oct 14 '14 at 16:40
  • Can't use powershell unfortunately – Bread Oct 14 '14 at 19:26
  • JSOM and CSOM are very similar. Chances are, methods available in CSOM are available in JSOM; the naming conventions are just different (get_property/set_property instead of GetProperty/SetProperty). – wjervis Oct 15 '14 at 13:33
3

Use SP.SecurableObject.breakRoleInheritance method to create unique role assignments for Folder.

How to create unique role assignments for a Folder using JSOM

function getFolder(url,success,error) {

   var ctx = SP.ClientContext.get_current(); 
   var folder = ctx.get_web().getFolderByServerRelativeUrl(url);   
   ctx.load(folder,'ListItemAllFields'); 
   ctx.executeQueryAsync(
      function () {
         success(folder);
      }, 
      error
   );
}

function breakFolderRoleInheritance(url,success,error) 
{
    getFolder(url,function(folder){
       var ctx = SP.ClientContext.get_current();  
       var folderItem = folder.get_listItemAllFields();  
       folderItem.breakRoleInheritance(false,false);

       ctx.executeQueryAsync(
         function () {
           success(folder);
         }, 
         error);
    },
    error);
}

Usage

Set unique role assignments for a folder named Orders located in Documents library:

breakFolderRoleInheritance('/Documents/Orders',
  function(folder){
      var folderItem = folder.get_listItemAllFields();  
      console.log(String.format('Folder {0} has unique permissions',folderItem.get_item('Title')));
  },
  function(sender,args){
      console.log(args.get_message());
  }
);
  • Thanks! I'll give this a shot and let you know how it goes. – Bread Oct 15 '14 at 13:24
  • Have you used this before in SP2010? It looks like the get_listItemAllFields() function is only available in 2013. – Bread Oct 15 '14 at 21:36
  • 1
    listItemAllFields should be in 2010 too: msdn.microsoft.com/en-us/library/office/… , did you load the property like he did: ctx.load(folder, 'ListItemAllFields') – Anders Aune Oct 17 '14 at 7:40

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.