8

I have a custom button on ribbon for document library and need to disable it if current SPListItem is an empty folder.

To disable button I use EnabledScript attribute of CommandUIHandler. I can get current SP.ListItem in JS COM by next code:

var clientContext = new SP.ClientContext();
var list = clientContext.get_web().get_lists().getById({ListID});  
var item = list.getItemById({ItemID});

And then I can detect if item is folder or file by its SP.ListItem.fileSystemObjectType Property

var fsoType = item.get_fileSystemObjectType();

And in case of File I can get file by SP.ListItem.file property. But I can't get SP.Folder if fsoType is Folder. How could I get it?

Then simply I could check SP.Folder.files and SP.Folder.folders roperties.

4

You can get all the folders with the following:

function GetFolders() {
  var context = new SP.ClientContext.get_current();
  var web = context.get_web();
  var list = web.get_lists().getByTitle('DocumentLibraryName');
  var query = SP.CamlQuery.createAllFoldersQuery();
  var allItems = list.getItems(query);
  context.load(allItems);
  context.executeQueryAsync(Function.createDelegate(this, this.success), 
      Function.createDelegate(this, this.failed));
}

function success() {
  var FolderNames = "";
  var ListEnumerator = this.allItems.getEnumerator();
  while (ListEnumerator.moveNext()) {
    var currentItem = ListEnumerator.get_current();
    var _contentType = currentItem.get_fileSystemObjectType();
    if(currentItem.get_fileSystemObjectType() == '1') {
      FolderNames  += currentItem.get_item('Title')+ '\n';
    }
  }
  alert(FolderNames);
}

function failed(sender, args) {
  alert("failed. Message:" + args.get_message());
}
0

If fsoType = Folder then I guess item is the SP.Folder already and you can use item.get_files();

Before you try to get those properties a clientContext.executeQueryAsync() is needed.

0

You need to check the value of the item.get_fileSystemObjectType();

Here are all the possible values: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.filesystemobjecttype.aspx

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.