0

I am making use of an SP.Service to retrieve the items of a particular list (Site Assets). I know the way to retrieve all the items under the list: using a caml query and the function createAllItemsQuery(). However I just need to retrieve the immediate child items.

Here is my actual structure of Site Assets:

      SiteAssets
          Folder1
             Folder1.1
             Folder1.2
          Folder2
             Folder2.1
          Etc...

So at the moment I am retrieving absolutely all the items bellow SiteAssets, and I wish only to retrieve Folder1, Folder2, which are the immediate child items of the lists.

Following is the code that I am using:

ExecuteOrDelayUntilScriptLoaded(retrieveAllListProperties, "sp.js");

      function retrieveAllListProperties()
      {

        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var list = web.get_lists().getByTitle('Site Assets');
        var query = SP.CamlQuery.createAllItemsQuery();  <-------// The caml query I am using
        allItems = list.getItems(query);
        context.load(allItems, 'Include(DisplayName,Id)'); <-------// Loading the context of the items
        context.executeQueryAsync(Function.createDelegate(this, this.viewSuccess), Function.createDelegate(this, this.failed));
      }

      function viewSuccess() 
      {
        var TextFiled = "";
        var ListEnumerator = allItems.getEnumerator(); <-------// Getting all the items, I just need the immediate child items, not sub-child items
        while (ListEnumerator.moveNext()) 
        {
          var currentItem = ListEnumerator.get_current();
          var title = currentItem.get_displayName();
          var id = currentItem.get_id().toString();
          console.log("Title: " + title + " Id: " + id); <-------// I am printing at the moment the name of the child and it's id
        }
      }

      function failed(sender, args) 
      {
        console.log('failed. Message:' + args.get_message());
      }

Is it posible to retrieve only the immediate child items? Can someone help me here?

1 Answer 1

0

This doesn't seem to be using SP.Services -- just the Javascript Client Object Model:

I did however learn something interesting by looking in to this...

To get the behavior you want, use an empty string as the CamlQuery argument to getItems:

allItems = list.getItems("");

I was previously under the impression this did the same thing as using SP.CamlQuery.createAllItemsQuery() as the argument, but it seems the default scope (since the empty string just represents an empty query) is not recursive, while createAllItems is -- which makes sense logically, just not something I had put together before!

1
  • You are amazing! That indeed solved my problem. Thank you very much!
    – user31620
    Sep 2, 2014 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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