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
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)
return;
var list = context.get_web().get_lists().getById(listId);
var qry = createQueryForSelectedItems(selectedItemIds);
var result = list.getItems(qry);
context.load(result);
context.executeQueryAsync(
function() {
success(result);
},
error
);
}
function createQueryForSelectedItems(selectedItemIds){
var qry = new SP.CamlQuery();
var viewXml = "<View> \
<Query> \
<Where> \
<In> \
<FieldRef Name='ID'/> \
<Values>";
for (var idx in selectedItemIds)
{
viewXml += " <Value Type='Counter'>" + selectedItemIds[idx].id + "</Value>";
}
viewXml+= " </Values> \
</In> \
</Where> \
</Query> \
</View>";
qry.set_viewXml(viewXml);
return qry;
}
//Usage: print file name of selected items
getSelectedItems(function(items){
var e = items.getEnumerator();
while (e.moveNext()) {
var item = e.get_current();
console.log(item.get_item('FileLeafRef'));
}
},function(sender,args){
console.log('An error occured: ' + args.get_message());
});
Have you tried something like this?
<Query>
<Where>
<Eq>
<FieldRef Name='ID' />
<Value Type='Number'>{YourIdGoesHere}</Value>
</Eq>
</Where>
<OrderBy><FieldRef Name='FileLeafRef' /></OrderBy>
</Query>
<ViewFields>
<FieldRef Name='ID' />
<FieldRef Name='FileLeafRef' />
<FieldRef Name='FileDirRef' />
</ViewFields>
<QueryOptions>
<ViewAttributes Scope='Recursive' />
<OptimizeFor>FolderUrls</OptimizeFor>
</QueryOptions>
-
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);
itemDetails.ListItem=listItem;
clientContext.load(listItem);
items.push(itemDetails);
}
clientContext.executeQueryAsync(OnLoadSuccess,OnLoadFailed);
}
}
function OnLoadSuccess() {
for (var x = 0; x < items.length; x++) {
var itemDetails = items[x];
alert(itemDetails.ListItem.get_item('FileLeafRef'));
}
}
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