4

I need to build a CAML query at runtime based on user input. The CAML query could be as simple as

<Where>
  <Eq>
    <FieldRef Name="Status" />
    <Value Type="Text">Awaiting Completion</Value>
  </Eq>
</Where>

or might include multiple AND and OR like

<Where>
  <Or>
    <Or>
      <Eq>
        <FieldRef Name="Status" />
        <Value Type="Text">Awaiting Completion</Value>
      </Eq>
      <Eq>
        <FieldRef Name="Status" />
        <Value Type="Text">Ongoing</Value>
      </Eq>
    </Or>
    <Eq>
      <FieldRef Name="status" />
      <Value Type="Text">Postponed</Value>
    </Eq>
  </Or>
</Where>

The problem is that as soon as I add a third Status to check as in the query above it generates an SPException

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

A quick code snippet to reproduce the problem:

   using (SPSite spSite = new SPSite("http://sp2010"))
            {
                using (SPWeb spWeb = spSite.OpenWeb("/test"))
                {
                    SPQuery spQuery = new SPQuery();
                    spQuery.Query = "<Where><Or><Or><Eq><FieldRef Name=\"Status\" /><Value Type=\"Text\">Awaiting Completion</Value></Eq><Eq><FieldRef Name=\"Status\" /><Value Type=\"Text\">Ongoing</Value></Eq></Or><Eq><FieldRef Name=\"status\" /><Value Type=\"Text\">Postponed</Value></Eq></Or></Where>";                    
                    SPListItemCollection spListItemCollection = spWeb.Lists["Actions"].GetItems(spQuery);
                    foreach(SPListItem spListItem in spListItemCollection)
                    {
                        Console.WriteLine(spListItem.Title);
                    }
                }
            }

            Console.ReadKey();

Any ideas?

2
  • 3
    Maybe in 3rd <Eq> you should use "Status" instead of "status". Commented Mar 15, 2011 at 16:55
  • @Andriy Shvay: You should make this an answer.
    – Kit Menke
    Commented Mar 15, 2011 at 20:18

1 Answer 1

5

Ok, I'm turning my comment into answer.
Onse in a while we all are making a typos - to solve your problem you should use "Status" instead of "status" in 3rd <Eq>.

2
  • Ha! Brilliant I spent so long getting my xml balanced in pairs of ANDs and ORs that I completely looked past the typo. Thank you very much.
    – user2643
    Commented Mar 16, 2011 at 9:37
  • Therefore I use Camlex.NET (camlex.codeplex.com) in my c# code as well as something like U2U Query Builder to check and test CAML queries.
    – user656
    Commented Apr 8, 2011 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.