Home > SharePoint > SharePoint Lists.asmx CAML Queries

SharePoint Lists.asmx CAML Queries

Recently, i had been working with SharePoint’s webservices, particularly Lists.asmx which is used for working with lists and corresponding list data.

The Lists class exposes several methods wherein “GetListItems” method is the one that i used to interact with my custom list available in SharePoint. The “GetListItem” method accepts a series of parameters. “query” is one of the parameter which passes a CAML query (assigned to System.Xml.XmlNode object) to query the appropriate list.

When i wanted query 2 list fields, I didn’t face any issue and the XML looks like:

<Query>
	<Where>
		<And>
			<Geq>
				<FieldRef Name="Reported_x0020_On"/>
				<Value Type="DateTime">2010-03-15T00:00:00</Value>
			</Geq>
			<Lt>
				<FieldRef Name="Reported_x0020_On"/>
				<Value Type="DateTime">2010-03-16T00:00:00</Value>
			</Lt>
		</And>
	</Where>
</Query>

Now, when the need came to query 3 List fields, i wrote the CAML query as:

<Query>
	<Where>
		<And>
			<Geq>
				<FieldRef Name="Reported_x0020_On"/>
				<Value Type="DateTime">2010-03-15T00:00:00</Value>
			</Geq>
			<Lt>
				<FieldRef Name="Reported_x0020_On"/>
				<Value Type="DateTime">2010-03-16T00:00:00</Value>
			</Lt>
			<Eq>
				<FieldRef Name="Environment"/>
				<Value Type="Choice">UAT</Value>
			</Eq>
		</And>
	</Where>
</Query>

but this query resulted in the following exceptions when i tried calling the Lists webservice’s GetListItems method:

Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.

Cannot complete this action.
Please try again.0x80004005

after a lot of guesses, i realized the fact that we need to format the criteria in pairwise conditions, like the one given below (note: i have used 3 conditions here)

<Query>
	<Where>
		<And>
			<And>
				<Geq>
					<FieldRef Name="Reported_x0020_On"/>
					<Value Type="DateTime">2010-03-15T00:00:00</Value>
				</Geq>
				<Lt>
					<FieldRef Name="Reported_x0020_On"/>
					<Value Type="DateTime">2010-03-16T00:00:00</Value>
				</Lt>
			</And>
			<Eq>
				<FieldRef Name="Environment"/>
				<Value Type="Choice">DEV 1</Value>
			</Eq>
		</And>
	</Where>
</Query>

similarly, when i’m in need for building 4 search criteria. Remember to group your criteria in pairs.

<Query>
	<Where>
		<And>
			<And>
				<Geq>
					<FieldRef Name="Reported_x0020_On"/>
					<Value Type="DateTime">2010-03-15T00:00:00</Value>
				</Geq>
				<Lt>
					<FieldRef Name="Reported_x0020_On"/>
					<Value Type="DateTime">2010-03-16T00:00:00</Value>
				</Lt>
			</And>
			<And>
				<Eq>
					<FieldRef Name="Environment"/>
					<Value Type="Choice">UAT</Value>
				</Eq>
				<Eq>
					<FieldRef Name="Resolved"/>
					<Value Type="Choice">Yes</Value>
				</Eq>
			</And>
		</And>
	</Where>
</Query>

i haven’t found much help when i was researching on this exception and it really took most of my time in this research and thats when i tried to document it and let others know about it.

Advertisements
  1. Janis Braslins
    November 29, 2010 at 8:54 PM

    Thank you so much for this post. I spent almost a day beating my head against this issue before I stumbled onto this post. Thank you!

  2. Darryl
    December 8, 2010 at 3:40 PM

    Basically, the allows two options, as does the tag. If you need more then two, you have to nest them ( inside of an , inside of an ).

  3. Darryl
    December 8, 2010 at 3:41 PM

    Basically, the “” allows two options, as does the “” tag. If you need more then two, you have to nest them (“” inside of an “”, “” inside of an “”).

  4. Darryl
    December 9, 2010 at 7:24 PM

    Geez, this commenting sections sucks. Lets try this again.

    Basically, the AND allows two options, as does the OR tag. If you need more then two, you have to nest them (AND inside of an AND, OR inside of an OR).

  5. July 13, 2011 at 9:40 PM

    I enjoy your work , appreciate it for all the great content .

  6. Dima
    August 21, 2011 at 5:40 PM

    Thanks, it helped me a lot.

  7. November 15, 2011 at 10:24 AM

    Thanks, after a few hours of endless searching your article put me on the right track.

  1. March 16, 2011 at 2:14 AM
    super Joe

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

%d bloggers like this: