0

I have a list of x people I am searching for in a SharePoint 2010 list. How do I build the or statement in XML similar to the if statement

if( person1 || person2 || person3 || ... personx)

Since every <Or> has to have exactly 2 children, no more, no less, I have this so far:

query.InnerXml = "<Where>" +
                     "<Or>" +
                        "<Or>" +
                        "<Contains>" +
                            "<FieldRef Name=\"Member\" />" +
                            "<Value Type=\"Text\">" +
                                 "Person1" +
                            "</Value>" +
                        "</Contains>" +
                        "<Contains>" +
                            "<FieldRef Name=\"Member\" />" +
                            "<Value Type=\"Text\">" +
                                 "Person2" +
                            "</Value>" +
                         "</Contains>" +
                     "</Or>" +
                     "<Contains>" +
                         "<FieldRef Name=\"Member\" />" +
                         "<Value Type=\"Text\">" +
                              "Person3" +
                         "</Value>" +
                     "</Contains>" +
                 "</Or></Where>";

But I cant figure out a good way to make all the other or statements while looping through my list of x people

1 Answers1

2

SharePoint 2010 added an In clause, which simplifies this.

Here's a helper method to put all of the people into Value wrappers:

public static string ValuesAsCAML(IEnumerable<string> values, string type)
{
    StringBuilder output = new StringBuilder();
    foreach (string value in values)
    {
        output.AppendFormat("<Value Type=`{0}`>{1}</Value>", type, value);
    }

    return output.ToString();
}

With that you can now write:

var people = new[] { "Person1", "Person2", "Person3" };
string values = ValuesAsCAML(people, "Text");

var query = string.Format(
@"<Where>
    <In>
        <FieldRef Name='Member` />
        <Values>
            {0}
        </Values>
    </In>
</Where>"
, values);
Servy
  • 202,030
  • 26
  • 332
  • 449