Executing SPQuery Queries and the U2U CAML Query Builder

If you are querying against any list using the SPQuery method, then most likely you are using the U2U CAML Query Builder. And if you are not, why? (This is old news! It’s been around since SharePoint 2003) This utility is always one of the first installations to any of my development environments.

This utility makes your life much easier by allowing you to build and test your queries directly against SharePoint, before debugging them, through code. And you can also do it remotely via web services, so you do not have to install and run this directly from the server itself.

A newer version was released last year as well that will install directly into your farm via a feature, to allow you to build, test, and save queries all from the SharePoint UI!

However, a common issue takes place when you copy the query directly from the U2U CAML Query Builder (which works just fine in the utility itself), in that no matter what conditions you have specified for your query, you always retrieve all list items. If this is the first time you have run into this – you have no doubt done a little of the timeless developer troubleshooting method

  1. Bang head against desk
  2. Repeat

You need not worry however, this is a quick fix. The U2U CAML Query Builder inserts <Query> </Query> tags around the query itself. In your code, just drop these start and end tags from your query, and you should now be returning your expected results.

image

 

Advertisement

About Geoff Varosky
Geoff Varosky is a Senior Architect for Insight, based out of Watertown, MA. He has been architecting and developing web based applications his entire career, and has been working with SharePoint for the past 15 years. Geoff is an active member of the SharePoint community, Co-Founder and Co-Organizer of the Boston Area SharePoint Users Group, co-founder for the Boston Office 365 Users Group, co-organizer for SharePoint Saturday Boston and speaks regularly at SharePoint events and user groups.

2 Responses to Executing SPQuery Queries and the U2U CAML Query Builder

  1. saranga says:

    Nice !!!!!!!!SpQuery and CAML in Sharepoint, it is simple.Try this too,http://sarangasl.blogspot.com/2009/10/caml-spquery-in-sharepoint.html

  2. pjohnraj says:

    I find the answer is

    I change the query like

    query.Query = string.Concat(“”, “”, “” + competencyAssessedUnit + “”, “”); SPListItemCollection items = list.GetItems(query);

    Just replace string to Text

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: