3

I am writing code for client side object model, and I want to retrieve items with specific title, like this:

 var context = SP.ClientContext.get_current(); var web = context.get_web(); var lists = web.get_lists(); var productList = web.get_lists().getByTitle("Products"); var query = new SP.CamlQuery(); query.set_viewXml = "<View><Query>" + "<Where><Eq>" + "<FieldRef Name='Title' />" + "<Value Type='Text'>Acer</Value>" + "</Eq></Where>" + "</Query></View>" ; var items = productList.getItems(query); context.load(items, 'Include(Title)'); context.load(lists, 'Include(Title, Fields.Include(Title))'); context.load(web, "Title"); context.executeQueryAsync(success, fail); 

It's retrieving everything, and the set_viewXml is ignored. Any help?

3 Answers 3

1

Use single quotes instead of double quotes while using Include. That is, change this line context.load(items, "Include(Title)"); to context.load(items, 'Include(Title)');

UPDATE

As mentioned by @Per, set_viewXml is a method and you are using it as property. Try this:

query.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'Title\'/>' + '<Value Type=\'Text\'>Acer</Value></Eq></Where></Query></View>'); 
3
  • Hi Nadeem, Thanks for your reply, the problem is with the query itself, it brings all items regardless of what's in the where condition, if I put a value that doesn't even exist, it will bring the items as well, I edited my question to reflect the code am currently using. Thanks for any suggestion Commented Apr 29, 2014 at 11:13
  • 1
    See my updated answer, set_viewXml is a method not property. Commented Apr 29, 2014 at 11:25
  • 2
    Why would single or double quotes matter? A string by either notation is still a string in JavaScript. Commented Aug 20, 2014 at 15:11
5

set_viewXml is a function and it requires a <View> element around the <Query> element so your code should be:

var context = SP.ClientContext.get_current(); var web = context.get_web(); var lists = web.get_lists(); var productList = web.get_lists().getByTitle("Products"); var query = new SP.CamlQuery(); query.set_viewXml("<View>" + "<Query>" + "<Where><Eq>" + "<FieldRef Name='Title' />" + "<Value Type='Text'>Acer</Value>" + "</Eq></Where>" + "</Query>" + "</View>"); var items = productList.getItems(query); context.load(items, "Include(Title)"); context.executeQueryAsync(success, fail); 
1
  • Also with that, didn't work. Commented Apr 28, 2014 at 19:05
0

However, I recheck the code, I found the issue. I had to supply the RowFilter option and it worked.

Here is the code for that:

function updateItems() { var queryMainFirst = ""; var queryFilter = ""; var rowLimit = ""; var queryMainLast = ""; //Get All the Selected IDs var listItemIds = decodeURIComponent(getQueryStringParameter("SPListItemId")); var delimiterlistItemIds = listItemIds.split(','); //Construct Dynamic CAML Query for (var i = 0; i < delimiterlistItemIds.length ; i++) { queryMainFirst = "<View><Where><Or>"; queryMainMiddle = "</Or></Where>" rowLimit = delimiterlistItemIds.length; queryMainLast = "</View>"; listItemIds = decodeURIComponent(getQueryStringParameter("SPListItemId")); queryFilter += "<Eq><FieldRef Name='ID' /><Value Type='Counter'>" + delimiterlistItemIds[i] + "</Value></Eq>"; } rowLimit = "<RowLimit>" + rowLimit + "</RowLimit>" var finalQuery = queryMainFirst.concat(queryFilter, queryMainMiddle, rowLimit, queryMainLast); //Get the Host Web Objects and update Workflow Tasks items. ; hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl")); clientContext = SP.ClientContext.get_current(); var hostContext = new SP.AppContextSite(clientContext, hostUrl); var web = hostContext.get_web(); listWF = web.get_lists().getByTitle("Workflow Tasks"); //Create CAML Object var query = new SP.CamlQuery(); //query.set_viewXml("<View><Where><Or><Eq><FieldRef Name='ID' /><Value Type='Counter'>10</Value></Eq></Or></Where><RowLimit>1</RowLimit></View>"); var query = new SP.CamlQuery(); var formattedQuery = String.format(finalQuery) query.set_viewXml(formattedQuery); collListItem = listWF.getItems(query); clientContext.load(collListItem); clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed); 

}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.