SP.CamlQuery() needs "<View><Query>"

Nov 13, 2013 at 1:10 PM
Edited Nov 13, 2013 at 1:11 PM
Just a notification:
I somebody would like to use camljs to build queries for the SP2010 JSCOM (SP.CamlQuery()) the query generated by camljs need to be wrapped with "View><Query>" ... "</Query></View>".
Otherwise the server will ignore it and returns the full list.
The funny thing that the server doesn't send any error message, therefore I didn't understand for days what's wrong with my query. :(
Nov 13, 2013 at 1:26 PM
Oh sorry, I should have included this into the samples or documentation as a reminder! Will do it ASAP. Thanks for the heads up!
Oct 11, 2014 at 12:56 AM
Edited Oct 11, 2014 at 12:58 AM
Starting from release 2.5, there are two changes related to this:
  • View element is now supported by CamlJs. So if you write something like this:
new CamlBuilder().View().Query().Where().TextField('foo').Contains('bar').ToString();
, then you will get View & Query elements in the resulting string.
  • .ToCamlQuery() method is added as alternative to the .ToString() finalization. Produces ready-to-use SP.CamlQuery object
Nov 17, 2014 at 12:23 PM
Hi omlin,

Should .ToCamlQuery() method work with SP2013? Because now its not working. For example:
new CamlBuilder().Where().IntegerField('ID').EqualTo(1).ToString()
returns "<Where><Eq><FieldRef Name="ID" /><Value Type="Integer">1</Value></Eq></Where>"
new CamlBuilder().Where().IntegerField('ID').EqualTo(1).ToCamlQuery()
returns object where viewXml parameter is wrapped into "<View><Query>":
SP.CamlQuery {$15_1: true, $l_1: "<View><Query><Where><Eq><FieldRef Name="ID" /><Val…e="Integer">1</Value></Eq></Where></Query></View>", $2Q_1: null, $2c_1: null, get_datesInUtc: function…}
As a result this query always returns nothing.
Nov 17, 2014 at 12:36 PM
Sorry, I guess it's my mistake.
We need to use new CamlBuilder().View().Query().Where().IntegerField('ID').EqualTo(1).ToString() or new CamlBuilder().Where().IntegerField('ID').EqualTo(1).ToCamlQuery(), right? It doesn't depend from SP version?
Nov 18, 2014 at 3:36 PM
Edited Nov 18, 2014 at 3:37 PM

Just to clarify one more time:
  • ToString returns "<Where>..</Where>", if it was created like this: new CamlBuilder().Where()...ToString()
  • ToString returns "<View>..</View>", if it was created like this: new CamlBuilder().View()...ToString()
  • ToCamlQuery always returns "<View>..</View>" in viewXml property of the SP.CamlQuery object, because SP.CamlQuery viewXml property requires View-based query. So if query was created via new CamlBuilder().Where()...ToString(), then it just wraps the result into view.
This doesn't anyhow depend on SharePoint version. ToCamlQuery should work in SP2010 and SP2013; ToString works in SP2007, SP2010 and SP2013.

At least this is how it should work. Bugs happen, you know :)
So if you find that something is not as described, just tell me, and I fix it.