With jquery and spservices, I use getSelectedItems to retrieve the IDs of a few documents. Can I have a loop which goes through a caml query for each of those IDs to retrieve the file name (FileLeafRef, I believe)? I've only used caml query within small console applications for a singular item, so I'm not entirely sure if this is possible.

3 Answers 3


The following example demonstrates how to retrieve an additional information about selected items:

function getSelectedItems(success,error)
    var context = SP.ClientContext.get_current();
    var listId = SP.ListOperation.Selection.getSelectedList(); //selected list Id
    var selectedItemIds = SP.ListOperation.Selection.getSelectedItems(context); //selected Items Ids
    if(selectedItemIds.length == 0)

    var list = context.get_web().get_lists().getById(listId);
    var qry = createQueryForSelectedItems(selectedItemIds);
    var result = list.getItems(qry);
       function() {


function createQueryForSelectedItems(selectedItemIds){
    var qry = new SP.CamlQuery();
    var viewXml = "<View> \
                     <Query> \
                       <Where> \
                         <In>  \
                          <FieldRef Name='ID'/> \
    for (var idx in selectedItemIds)
        viewXml += "         <Value Type='Counter'>" + selectedItemIds[idx].id + "</Value>";
    viewXml+= "            </Values> \
                         </In> \
                       </Where> \
                     </Query> \
    return qry;               

//Usage: print file name of selected items
    var e = items.getEnumerator();
    while (e.moveNext()) {
       var item = e.get_current();
    console.log('An error occured: ' + args.get_message());

Have you tried something like this?

         <FieldRef Name='ID' />
         <Value Type='Number'>{YourIdGoesHere}</Value>
   <OrderBy><FieldRef Name='FileLeafRef' /></OrderBy>
   <FieldRef Name='ID' />
   <FieldRef Name='FileLeafRef' />
   <FieldRef Name='FileDirRef' />
   <ViewAttributes Scope='Recursive' />

Check out this link for more details

  • Is this the proper format to return a set of results? I'm confused on how I would implement this query to store its results into an array or enumeration. Mar 25, 2014 at 15:42

You don't necessarily have to call Caml Query inside loop to get the FileLeafRef here since you already know the IDs of the Documents. You can use SP.List.getItemById(id) method.Please see below code to give you an idea of how to start with:

items = [];   
        function getSetListItem() {
            var clientContext = SP.ClientContext.get_current();
            if (clientContext != undefined && clientContext != null) {
                var webSite = clientContext.get_web();
                var list = webSite.get_lists().getByTitle("DocumentLibraryName");
                for (var i=1;i<=5;i++)//Change this to your loop condition
                var itemDetails = new Object;
                var listItem = list.getItemById(i);
        function OnLoadSuccess() {
              for (var x = 0; x < items.length; x++) {            

                 var itemDetails = items[x];


         function OnLoadFailed(sender, args) {
              alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
  • I considered going down this path, but I have too many functions that need the file names. With ctx.executeQueryAsync, I can't push the file name variable to a global variable so all of my work with the variable needs to be done inside there. Is the caml query approach not preferred? Mar 24, 2014 at 20:16

Your Answer

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

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