Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

CAML Query Failing

  Asked By: Fredrick    Date: Apr 23    Category: Sharepoint    Views: 6134

I have a CAML query against a contacts list.

The orignal query is as follows:

<Where>
<And>
<Eq>
<FieldRef Name='Company' />
<Value Type='Text'>" + i["Company"] + "</Value>
</Eq>
<Eq>
<FieldRef Name='Hospital_x0020_Associate_x0020_R' />
<Value Type='Choice'>Program Director</Value>
</Eq>
</And>
</Where>

This query works just fine, but when I try to add a 3rd condition to
the query like so:

<Where>
<And>
<Eq>
<FieldRef Name='Company' />
<Value Type='Text'>" + i["Company"] + "</Value>
</Eq>
<Eq>
<FieldRef Name='Hospital_x0020_Associate_x0020_R' />
<Value Type='Choice'>Program Director</Value>
</Eq>
<Eq>
<FieldRef Name='Market' />
<Value Type='String'>" + market + "</value>
</Eq>
</And>
</Where>

I get the following error:

Cannot complete this action. Please try again.

I did some research and found that to add the 3rd condition I need to
add another <And> like so:

<Where>
<And>
<Eq>
<FieldRef Name='Company' />
<Value Type='Text'>" + i["Company"] + "</Value>
</Eq>
<Eq>
<FieldRef Name='Hospital_x0020_Associate_x0020_R' />
<Value Type='Choice'>Program Director</Value>
</Eq>
</And>
<And>
<Eq>
<FieldRef Name='Market' />
<Value Type='String'>" + market + "</value>
</Eq>
</And>
</Where>

Doing it this way gives me the following error:

One or more field types are not installed properly. Go to the list
settings page to delete these fields.

My question is: What is the correct way to formulate a CAML query with
more than 2 conditions, and can't one <And> handle all conditions.

Also is there any kind of documentation for writing CAML queries? The
only useful documentation I've found is on MSDN, and in my opinion is
poor at best.

Share: 

 

7 Answers Found

 
Answer #1    Answered By: Constance Guerrero     Answered On: Apr 23

You almost got it, try this:

<Where>
<And>
<And>
<Eq>
<FieldRef Name='Company' />
<Value Type='Text'>" + i["Company"] + "</Value>
</Eq>
<Eq>
<FieldRef Name='Hospital_x0020_Associate_x0020_R' />
<Value Type='Choice'>Program Director</Value>
</Eq>
</And>
<Eq>
<FieldRef Name='Market' />
<Value Type='String'>" + market  + "</value>
</Eq>
</And>
</Where>

 
Answer #2    Answered By: Chandrabhan Konwar     Answered On: Apr 23

I got it to work, I don't know why the corrected query  didn't work the
first couple of times, but it is working now and I really appreciate
your help.

Do either of you (or anyone else for that matter) know of a good CAML
syntax resource? I have a background in SQL, and a lot of the CAML
syntax doesn't make sense to me at the first glance.

 
Answer #3    Answered By: Tina Owens     Answered On: Apr 23

I tend to use the U2U CAML Query builder. You can find it here.

http://www.u2u.be/res/CamlQueryBuilder.aspx

 
Answer #4    Answered By: Lesley Tate     Answered On: Apr 23

I am trying to query  a contacts  list in share point with 3 conditions.

The following query works  as expected 2 conditions:

<Where>
<And>
<Eq>
<FieldRef Name='Company' />
<Value Type='Text'>" + i["Company"] + "</Value>
</Eq>
<Eq>
<FieldRef Name='Hospital_x0020_Associate_x0020_R' />
<Value Type='Choice'>Program Director</Value>
</Eq>
</And>
</Where>

When I try to add  a 3rd  condition to the <And> clause using the
following query:

<Where>
<And>
<Eq>
<FieldRef Name='Company' />
<Value Type='Text'>" + i["Company"] + "</Value>
</Eq>
<Eq>
<FieldRef Name='Hospital_x0020_Associate_x0020_R' />
<Value Type='Choice'>Program Director</Value>
</Eq>
<Eq>
<FieldRef Name='Market' />
<Value Type='Text'>" + market  + "<Value>
</Eq>
</And>
</Where>

I get the following error:

Cannot complete  this action. Please try again.

I did some digging and found  that the 'proper' syntax for more than 2
conditions is as follows:

<Where>
<And>
<Eq>
<FieldRef Name='Company' />
<Value Type='Text'>" + i["Company"] + "</Value>
</Eq>
<Eq>
<FieldRef Name='Hospital_x0020_Associate_x0020_R' />
<Value Type='Choice'>Program Director</Value>
</Eq>
</And>
<And>
<Eq>
<FieldRef Name='Market' />
<Value Type='Text'>" + market + "<Value>
</Eq>
</And>
</Where>

But when I run this query I get the following error:

One or more field  types are not installed  properly. Go to the list
settings page  to delete  these fields.

So I my question  is: What is the proper syntax for a CAML query with
more than 3 conditions?

Also does anyone know of a good reference for CAML queries? The only
documentation of any kind  that I have seen is on MSDN and that
documentation seems pretty sparse.

 
Answer #5    Answered By: Vinay Thakur     Answered On: Apr 23

Your field  types seems ok. I think there is a small error  in the
construction of the And clauses.

The proper syntax for your query  is:

<Where>
<And>
<And>
<Eq>
<FieldRef Name='Company' />
<Value Type='Text'>" + i["Company"] + "</Value>
</Eq>
<Eq>
<FieldRef Name='Hospital_x0020_Associate_x0020_R' />
<Value Type='Choice'>Program Director</Value>
</Eq>
</And>
<Eq>
<FieldRef Name='Market' />
<Value Type='Text'>" + market  + "<Value>
</Eq>
</And>
</Where>

 
Answer #6    Answered By: Lynn Mann     Answered On: Apr 23

When I try the query  that you have built, it fails and I get the
following error:

Cannot complete  this action. Please try again.

I think I'm having a hard time understanding why you need to have 2
conditions in one <And> clause and a third condition  in another.

 
Answer #7    Answered By: Damini Dande     Answered On: Apr 23

As the syntax of the CAML query  is correct  now, I can only start
guessing what's wrong.

I suppose that you are trying to build this query string in f.e. C#.
Have you already checked what's coming out of i["CustomerName"]? Is
it possible this returns an object? in that case, try to add  .ToString
() to it and retest your query.

 
Didn't find what you were looking for? Find more on CAML Query Failing Or get search suggestion and latest updates.




Tagged: