Reputation: 29
I am new to caml query and have been struggling for this. I need the last modified List Item. Only one Item. That means it should be orderby 'modified' and rowlimit should be 1.
But only rowlimit part of my query is working. Not the orderby part.
This is my Query :
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'Modified\' Ascending="FALSE"/></OrderBy><RowLimit>1</RowLimit></Query></View>";')
I dont know where I am going wrong. I even tried removing the Query tags in the above mentioned query.
The query is working, its getting only one record. orderby isnt working i believe.
This is in jQuery. I have written in a function and am calling that function in my Ready function.
Please help me.
Thanks.
Upvotes: 2
Views: 31687
Reputation: 161
Here there is a full sample, Ascending='True' or Ascending='False' it's case sensitive
<View>
<Query>
<Where>
<Eq>
<FieldRef Name='OrderNum' />
<Value Type='Number'>90696</Value>
</Eq>
</Where>
<OrderBy>
<FieldRef Name='Modified' Ascending='False'/>
</OrderBy>
</Query>
<ViewFields>
<FieldRef Name='Title' />
<FieldRef Name='ID' />
</ViewFields>
<RowLimit>10</RowLimit>
Upvotes: 2
Reputation: 430
AymKdn is correct, the OrderBy clause should be outside of the Query clause in your Caml. Here's an example:
caml.set_viewXml('<View><OrderBy><FieldRef Name="Modified" Ascending="False"/></OrderBy><RowLimit>1</RowLimit></View>');
The Query clause is used for filtering (Where clause).
Upvotes: 0
Reputation: 59378
In fact, in your example the query returns all results since query contains some errors.
The line:
camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'Modified\' Ascending="FALSE"/></OrderBy><RowLimit>1</RowLimit></Query></View>";')
should be replaced with:
camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name="Modified" Ascending="FALSE"/></OrderBy></Query><RowLimit>1</RowLimit></View>');
Example: how to get the last item
function getLastItem(listTitle,Success,Error){
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle(listTitle);
var query = new SP.CamlQuery();
query.set_viewXml('<View><Query><OrderBy><FieldRef Name="Modified" Ascending="FALSE"/></OrderBy></Query><RowLimit>1</RowLimit></View>');
var items = list.getItems(query);
ctx.load(items);
ctx.executeQueryAsync(
function() {
if(items.get_count() > 0) {
var item = items.getItemAtIndex(0);
Success(item);
}
else
{
Success(null);
}
},
Error
);
}
getLastItem('Contacts',function(item){
console.log(item.get_item('Modified'));
},function(sender,args){
console.log(args.get_message());
});
Upvotes: 7
Reputation: 3927
Rowlimit must be put outside the query. Actually I'm using SharepointPlus (a JavaScript API to deal with Sharepoint) that creates automatically the query for me :-)
So the XML code sent to the server should look like this:
<GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<listName>Your list</listName>
<viewName></viewName>
<query>
<Query>
<OrderBy>
<FieldRef Name="Status" Ascending="false"></FieldRef>
</OrderBy>
</Query>
</query>
<rowLimit>1</rowLimit>
<viewFields>
<ViewFields Properties="True">
<FieldRef Name="ID"></FieldRef>
</ViewFields>
</viewFields>
<queryOptions>
<QueryOptions>
<DateInUtc>False</DateInUtc>
<Paging ListItemCollectionPositionNext=""></Paging>
<IncludeAttachmentUrls>True</IncludeAttachmentUrls>
<IncludeMandatoryColumns>False</IncludeMandatoryColumns>
<ExpandUserField>False</ExpandUserField>
<ViewAttributes Scope="Recursive"></ViewAttributes>
</QueryOptions>
</queryOptions>
</GetListItems>
Upvotes: 2