SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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.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';

function failed(sender, args) {
  alert("failed. Message:" + args.get_message());
share|improve this answer

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

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

Here are all the possible values:

share|improve this answer

Your Answer


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.