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?

SharePoint Freak
  • 1,378
  • 7
  • 26
  • 39

3 Answers3

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);
Arsalan Adam Khatri
  • 14,531
  • 3
  • 36
  • 59
Per Jakobsen
  • 32,409
  • 1
  • 33
  • 62
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>');
Nadeem Yousuf-AIS
  • 18,707
  • 4
  • 28
  • 59
  • 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 – SharePoint Freak Apr 29 '14 at 11:13
  • 1
    See my updated answer, set_viewXml is a method not property. – Nadeem Yousuf-AIS Apr 29 '14 at 11:25
  • 2
    Why would single or double quotes matter? A string by either notation is still a string in JavaScript. – John-M Aug 20 '14 at 15:11
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);

}

Mohamed Derhalli
  • 2,622
  • 11
  • 18