>
Blog
Book
Portfolio
Search

7/9/2013

5829 Views // 0 Comments // Not Rated

SharePoint 2013 Quick Tips: CSOM Caml Queries Not Filtering

For those of ua51191f0-25f1-48d8-9436-2f65ced1b74dPoint development careers intimate with writing Caml queries, we've gotten used to them being fragile little beasts. Screw up a column name? Exception. Miss a quote or unbalance a bracket? Bomb. And that's before even attempting to group two clauses with an "Or" tag.

But the upshot to this is no false positives. If your query runs, it's a good bet it ran successfully, respecting any "Where" or "OrderBy" clauses it might contain. However, in 2013's amped up CSOM library, this is no longer the case; Caml queries need to be a bit more properly formed to ensure that your filters, specially "Where" clauses, are considered.

The way to do this is to explicitly wrap your ViewXml with "View" and "Query" tags. This isn't required in farm solutions, nor was it required in 2010. But in 2013 CSOM, if you omit this previously-superfluous markup, your technically incorrect Caml will not crash and burn like it should; it will quietly pull back the entire list. This could land you in a much worse situation than dealing with an easily-remediated "column does not exist" error.

Let's take a look:

Code Listing 1

  1. CamlQuery query = new CamlQuery()
  2. {
  3. ViewXml = string.Format("<View><Query><Where><Eq><FieldRef Name='{0}' /><Value Type='URL'>{1}</Value></Eq></Where></Query></View>", someInternalColumnName, someUrl)
  4. };

Like I said: just wrap your Caml with "View" and "Query" tags and your filters will be able to do their job. I'm actually a fan of this move by Microsoft; issuing fragments of XML to a Caml query never quite felt right to me. If the schema calls for a root element named "View" then why not just type out the extra thirteen characters to make it so? I like things neat and proper, and have no problem burning a few extra calories to get my code up to that next level of elegance when budget and timeline are unaffected.

Have fun querying!

No Tags

No Files

No Thoughts

Your Thoughts?

You need to login with Twitter to share a Thought on this post.


Loading...