views:

31

answers:

2

Hi All When I use CAML to filter my ListView, it ok when I use this CAML:

  query = "<Where><Or>" +
                    "<And>" +
                       " <Eq>" +
                            "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                            "<Value Type=\"Text\">Tag</Value>" +
                         "</Eq>" +
                         "<IsNotNull>" +
                         "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                         "</IsNotNull>" +
                    "</And>" +
                    "<And>" +
                       " <Eq>" +
                            "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                            "<Value Type=\"Text\">Word</Value>" +
                         "</Eq>" +
                         "<IsNotNull>" +
                         "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                         "</IsNotNull>" +
                    "</And>" + 
                    "</Or>" +
                 "</Where>";

It can work correctly.

But when i use the CAML in below, it will show Error in the ListView

query = "<Where><Or>" +
                    "<And>" +
                       " <Eq>" +
                            "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                            "<Value Type=\"Text\">Tag</Value>" +
                         "</Eq>" +
                         "<IsNotNull>" +
                         "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                         "</IsNotNull>" +
                    "</And>" +
                    "<And>" +
                       " <Eq>" +
                            "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                            "<Value Type=\"Text\">Word</Value>" +
                         "</Eq>" +
                         "<IsNotNull>" +
                         "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                         "</IsNotNull>" +
                    "</And>" + 
                    "<And>" +
                       " <Eq>" +
                            "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                            "<Value Type=\"Text\">Word Test</Value>" +
                         "</Eq>" +
                         "<IsNotNull>" +
                         "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                         "</IsNotNull>" +
                    "</And>" +
                    "</Or>" +
                 "</Where>";

I just add a filter on the second CAML, why it can't work?

Anybody can help?

Thanks kedk

A: 

The operator can only take 2 operands, not 3 (unlike when you use it in the code)

Therefore, you will need to split your query, or nest the second operator :

"<Where><Or>" +
                "<And>" +
                   " <Eq>" +
                        "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                        "<Value Type=\"Text\">Tag</Value>" +
                     "</Eq>" +
                     "<IsNotNull>" +
                     "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                     "</IsNotNull>" +
                "</And>" +
                //Nest the second part of the Query by putting another <And> here
                "<And>" +
                  "<And>" +
                   " <Eq>" +
                        "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                        "<Value Type=\"Text\">Word</Value>" +
                     "</Eq>" +
                     "<IsNotNull>" +
                     "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                     "</IsNotNull>" +
                  "</And>" + 
                  "<And>" +
                   " <Eq>" +
                        "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                        "<Value Type=\"Text\">Word Test</Value>" +
                     "</Eq>" +
                     "<IsNotNull>" +
                     "<FieldRef Name=\"TagForDiscussion\"></FieldRef>" +
                     "</IsNotNull>" +
                  "</And>" +
                "</And>" +
                "</Or>" +
             "</Where>";
Hugo Migneron
Thanks you guys
A: 

Hi Kedk

You try to have 3 expressions within the <Or> element, but it only allows exactly two.

So you have to change:

<Or>
  <And>...</And>
  <And>...</And>
  <And>...</And>
</Or>

to

<Or>
  <Or>
     <And>...</And>
     <And>...</And>
  </Or>
  <And>...</And>
</Or>
Per Jakobsen