Hi can anyone show me how can I retieve list items based on 2 fields using ECMAscript CAML? I have a list where I have uid as number and city as text.
I want to retrieve all items where uid = 100 and city='A'.
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityTo return items from a list using ECMAScript (JavaScript, JScript), use the getItemById(id) function to return a single item, or use the getItems(query) function to return multiple items. You then use the load(clientObject) function to attain list item objects that represent the items.
Try the following:
var siteUrl = '/sites/MySiteCollection';
function retrieveListItems() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Announcements');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name=\'uid\'/><Value Type=\'Number\'>100</Value></Eq><Eq><FieldRef Name=\'city\'/><Value Type=\'Text\'>A</Value></Eq</And></Where></Query><RowLimit>10</RowLimit></View>');
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
listItemInfo += '\nID: ' + oListItem.get_id() +
'\nTitle: ' + oListItem.get_item('Title') +
'\nBody: ' + oListItem.get_item('Body');
}
alert(listItemInfo.toString());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
Reference: How to: Retrieve List Items Using JavaScript