CAML Query: Multiple Query Fields Issue

J

Jon F.

I need to Create a CAML Query Dynamically with VB to a Sharepoint
WebService GetListItems Method.

The User Could Select 1 to X Number of IDs from a list Box. For a
Database Id I would create the Query String Dynamically, but I can't
figure out how to do this with CAML queries


To Create Database SQL Query with VB would look something like this, (i
don't know if this code works, its just to show an example)
*************************
Dim SelectStr As String
Dim Element As String

SelectStr = "Select * from table where "

For Each Element in StringArray
SelectStr += "ID=" & Element & " or "
Next
Dim Idx As Integer = SelectStr = LastIndexOf(" or ")
SelectStr = SelectStr.Substring(0,Idx)
**************************
The Result is a sql String something like : "Select * from table where
ID=1 or ID=5 Or ID=7 Or ID=89"


The Problem I'm Having is Creating a CAML Query like the SQL Query
above. I can't figure out the pattern of where the <Or> Tag needs to go
based on the Number of Filter Fields. Example for two Fields the query
would look like:
<Where>
<Or>
<Eq>
<FieldRef Name='ID'/>
<Value Type='Number'>1</Value>
</Eq>
<Eq>
<FieldRef Name='ID'/>
<Value Type='Number'>5</Value>
</Eq>
</Or>
</Where>

But Placement of the <Or> Tag Changes as you add Fields:
<Where>
<Or> 'Added to Handle Third Field
<Or>
<Eq>
<FieldRef Name='ID'/>
<Value Type='Number'>1</Value>
</Eq>
<Eq>
<FieldRef Name='ID'/>
<Value Type='Number'>5</Value>
</Eq>
</Or>
<Eq>
<FieldRef Name='ID'/>
<Value Type='Number'>7</Value>
</Eq>
</Or>
</Where>


Thanks,
Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top