简体   繁体   中英

Sharepoint XML query from array

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM