SPQuery Vs CamlQuery

SP2013


Hello everyone,

Today I’ll talk about a mistake I’ve seen several times in the forums, namely the formatting of CAML queries.

When you want to use CAML queries, the two objects that are most often used are SPQuery and CamlQuery.

The SPQuery is used like this

    SPQuery oQuery = new SPQuery();

    oQuery.Query = “<Where><Geq><FieldRef Name=’Field2’/>” +

        “<Value Type=’Number’>1000</Value></Geq></Where>”;

And the CAMLQuery like this

CamlQuery camlQuery = new CamlQuery();

camlQuery.ViewXml = “<View><Query><Where><Geq><FieldRef Name=’Field2’/>” +

        “<Value Type=’Number’>1000</Value></Geq></Where></Query></View>”;

Seeing the two uses you probably already saw the difference, but for those who have not been paying attention, look carefully to the contents of the query.

With SPQuery, the query starts and ends with the « Where » tags while with CAMLQuery, you have  « View » and « Query » tags in extra !

It may sound easy but if you normally use an object and then the other, you can easily put any doubt except formatting the query.

More information on these two objects

SPQuery : http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.query.aspx

CAMLQuery : http://msdn.microsoft.com/en-us/library/ee534956(v=office.14).aspx

Hope this helps

Christopher.

Advertisements

2 thoughts on “SPQuery Vs CamlQuery

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s