Monday, June 20, 2011

CAML & C#

If you are querying a Sharepoint 2010 List with CAML using C#, sometimes you can face inconsistent results. One of the most popular ones is not working OrderBy statements. After querying the list in C# with OrderBy query, you may see that the incoming list is still in default order.

The solution here is simple: In case you are using Query tag in your query string, just remove it. Your querystring should start directly with Where or OrderBy tag and this should solve your problem.

If it still does not, you can add Type field to your OrderBy query. Type="Number" or Type="DateTime" helps CAML interpreter to understand howto sort it. (Well, it should automatically detect it from Sharepoint therotically, yes, but the real life is never easy in Sharepoint)

The last thing in your checklist can be that CAML is a case-sensitive language, you can think about checking your query syntax and capital letters.

No comments: