Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

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());
}
share|improve this answer
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.