CAML语法-Query写法
1.Geq(>=)
The Geq element is an arithmetic operator that means "greater than or equal to." It can be used within a Where element in a view definition.
<Query>
<Where>
<Or>
<IsNull>
<FieldRef Name="Expires" />
</IsNull>
<Geq>
<FieldRef Name="Expires" />
<Value Type="DateTime">
<Today />
</Value>
</Geq>
</Or>
</Where>
<OrderBy>
<FieldRef Name="Modified" Ascending="FALSE" />
</OrderBy>
</Query>
2.Eq(=)
The Eq element is an arithmetic operator that means "equal to" and is used within the Query element.
<Query>
<OrderBy>
<FieldRef Name="Modified" Ascending="FALSE"></FieldRef>
</OrderBy>
<Where>
<Or>
<Eq>
<FieldRef Name="Status"></FieldRef>
<Value Type="Text">Completed</Value>
</Eq>
<IsNull>
<FieldRef Name="Status"></FieldRef>
</IsNull>
</Or>
</Where>
</Query>
3.Gt(>)
The Gt element is an arithmetic operator that means "greater than." This element is used similarly to Eq and Lt.
<Query>
<Where>
<Or>
<IsNull>
<FieldRef Name="Deadline" />
</IsNull>
<Gt>
<FieldRef Name="Deadline" />
<Value Type="DateTime">
<Today />
</Value>
</Gt>
</Or>
</Where>
<OrderBy>
<FieldRef Name="Modified" Ascending="FALSE" />
</OrderBy>
</Query>
4.Lt(<)
The Lt element is an arithmetic operator that means "less than" and is used in queries in views. This element is used similarly to Eq and Gt.
<Query>
<Where>
<Or>
<IsNull>
<FieldRef Name="Deadline" />
</IsNull>
<Lt>
<FieldRef Name="Deadline" />
<Value Type="DateTime">
<Today />
</Value>
</Lt>
</Or>
</Where>
<OrderBy>
<FieldRef Name="Modified" Ascending="FALSE" />
</OrderBy>
</Query>
5.Neq(!=)
The Neq element is an arithmetic operator that means "not equal to" and is used in queries.
<Query>
<OrderBy>
<FieldRef Name="Modified" Ascending="FALSE"></FieldRef>
</OrderBy>
<Where>
<Or>
<Neq>
<FieldRef Name="Status"></FieldRef>
<Value Type="Text">Completed</Value>
</Neq>
<IsNull>
<FieldRef Name="Status"></FieldRef>
</IsNull>
</Or>
</Where>
</Query>
协作应用程序标记语言 (CAML)---Query语法示例
1、Or,Lt,Gt,OrderBy
<Query>
<Where>
<Or>
<Lt>
<FieldRef Name="Stock"/>
<Value Type="Number">
15
</Value>
</Lt>
<Gt>
<FieldRef Name="Price"/>
<Value Type="Currency">
20.00
</Value>
</Gt>
</Or>
</Where>
<OrderBy>
<FieldRef Name="Title"/>
</OrderBy>
</Query>
2、Eq
<Query>
<Where>
<Eq>
<FieldRef Name="ID"/>
<Value Type="Integer"><UserID/></Value>
</Eq>
</Where>
</Query>
3、And,Eq,Lt,Neq,And嵌套
<Query>
<Where>
<And>
<Eq>
<FieldRef Name="AssignedTo"/>
<Value Type="Integer">
<UserID/>
</Value>
</Eq>
<And>
<Lt>
<FieldRef Name="DueDate"/>
<Value Type="DateTime">
<Today/>
</Value>
</Lt>
<Neq>
<FieldRef Name="Status"/>
<Value Type="Choice">Completed</Value>
</Neq>
</And>
</And>
</Where>
</Query>
<Query>
<Where>
<And>
<Eq>
<FieldRef Name="AssignedTo"/>
<Value Type="Integer">
<UserID/>
</Value>
</Eq>
<And>
<Lt>
<FieldRef Name="DueDate"/>
<Value Type="DateTime">
<Today/>
</Value>
</Lt>
<Neq>
<FieldRef Name="Status"/>
<Value Type="Choice">Completed</Value>
</Neq>
</And>
</And>
</Where>
</Query>
7.5 下表是CAML查询的一些简单说明:
元素 说明
And 并且
BeginsWith 以某字符串开始的
Contains 包含某字符串
Eq 等于
FieldRef 一个字段的引用 (在GroupBy 中使用)
Geq 大于等于
GroupBy 分组
Gt 大于
IsNotNull 非空
IsNull 空
Leq 小于等于
Lt 小于
Neq 不等于
Now 当前时间
Or 或
OrderBy 排序
Today 今天的日期
TodayIso 今天的日期(ISO格式)
Where Where子句
http://www.cnblogs.com/cangqiong/archive/2010/12/19/1910808.html
查询List中Lookup类型的字段的值 http://tech.ddvip.com/2009-03/1237894483112156.html
Loopup类型的字段在数据库中相当于外键,数据格式如1;#title1;#2;#title2,查询时可以按照下面的CAML。
<Query>
<Where>
<Eq>
<FieldRef Name="ProjectID" />
<Value Type="Lookup">My Test Project 1</Value>
</Eq>
</Where>
</Query>
Value中只需要填写外键所对应的名称,而不用填写外键所对应的id。如果只知道id而不知道名称则可以使用下面的CAML。
<Query>
<Where>
<Eq>
<FieldRef Name="ProjectID" LookupId="TRUE" />
<Value Type="Text">3</Value>
</Eq>
</Where>
</Query>
如果列表中有子目录,即子folder. 需要设置 query 的 viewattributes
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='" + list.Fields["PR Workflow"].InternalName + "'/><Value Type='Text'>2</Value></Eq></Where>";
query.ViewAttributes = "Scope='RecursiveAll'";
http://social.technet.microsoft.com/Forums/lv-LV/sharepoint2010programming/thread/27c28b2d-c2fc-4b42-bf7c-8f3f1f533aea