Is this the proper Syntax for Caml Querys?

本小妞迷上赌 提交于 2019-12-11 09:48:55

问题


I have a working Caml Query like so:

<View><Query><Where><Eq><FieldRef Name=\'ptli_TravelersEmail\' /><Value Type=\'Text\'>' + payeename + '</Value></Eq></Where></Query></View>

...that retrieves "records" from a Sharepoint list where the value in the 'ptli_TravelersEmail' field equates to the value of the passed-in arg "payeename".

To add another clause to the query, to retrieve "records" where the former holds true but also where the value in the 'ptli_preparer' field equates to the value of the passed-in arg "username," do I need to repeat an entire "Where.Eq.FieldRef Name.Value..Value.Eq.Where" section, like this:

<View><Query><Where><Eq><FieldRef Name=\'ptli_TravelersEmail\' /><Value Type=\'Text\'>' + payeename + '</Value></Eq></Where><Where><Eq><FieldRef Name=\'ptli_preparer\' /><Value Type=\'Text\'>' + username + '</Value></Eq></Where></Query></View>

...or is my syntax off?

I could just try it and find out, I know, but the build/run/test process in Sharepoint takes quite awhile, and I'm hoping some Caml expert knows right off the bat.


回答1:


Here's the general format for a CAML query:

<View>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name="Internal_Name_of_field" />
                <Value Type="Text">The value to filter against</Value>
            </Eq>
        </Where>
    </Query>
</View>

<Eq> means "Equals." You can also use comparisons like <Neq> (Not Equals), <Lt> (Less than), <Leq> (Less than or equal to), <Gt> (Greater than), <Geq> (Greater than or equal to), <Contains>, <IsNull>, and <IsNotNull>.

When you want your CAML query to have multiple conditions, you can combine two of them inside a set of <And> tags (documented here).

<Where>
    <And>
        <Eq>
            <FieldRef Name="Internal_Name_of_field1" />
            <Value Type="Text">The value to filter against</Value>
        </Eq>
        <Eq>
            <FieldRef Name="Internal_Name_of_field2" />
            <Value Type="Text">The value to filter against</Value>
        </Eq>
    </And>
</Where>

You can nest <And> tags inside other <And> and <Or> tags to build arbitrarily complicated queries.

<Where>
    <And>
        <Eq>
            <FieldRef Name="Internal_Name_of_field1" />
            <Value Type="Text">The value to filter against</Value>
        </Eq>
        <And>
            <Eq>
                <FieldRef Name="Internal_Name_of_field2" />
                <Value Type="Text">The value to filter against</Value>
            </Eq>
            <Eq>
                <FieldRef Name="Internal_Name_of_field3" />
                <Value Type="Text">The value to filter against</Value>
            </Eq>
        </And>
    </And>
</Where>

The exact syntax used in the <Value> element can vary depending on the type of field being compared against. Type="Text" works for single line text fields, but lookup fields, date fields, and person or group fields have different syntax.

For more advanced CAML queries, note the placement of OrderBy and RowLimit elements:

<View>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name="Internal_Name_of_field" />
                <Value Type="Text">The value to filter against</Value>
            </Eq>
        </Where>
        <OrderBy>
            <FieldRef Name="Internal_Name_of_field" />
        </OrderBy>
    </Query>
    <RowLimit>500</RowLimit>
</View>


来源:https://stackoverflow.com/questions/33113993/is-this-the-proper-syntax-for-caml-querys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!