sharepoint的caml查询语句的小发现
2009-01-04 13:50 Virus-BeautyCode 阅读(4702) 评论(7) 编辑 收藏 举报
这个查询的caml我已经研究了一个礼拜了,可就是进展缓慢。
我的需求是一个不固定条件的查询,要组合四个不固定的条件,就是说可能是一个,2个,3个或者4个,
我的需求是一个不固定条件的查询,要组合四个不固定的条件,就是说可能是一个,2个,3个或者4个,
<Query>
<Where>
<And>
<And>
<Contains>
<FieldRef Name='Title' />
<Value Type='Text'>1</Value>
</Contains>
<Leq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2010-1-1 00:00:00Z</Value>
</Leq>
</And>
<Geq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2009-1-4 00:00:00Z</Value>
</Geq>
</And>
</Where>
</Query>
<ViewFields>
<FieldRef Name='Title' />
</ViewFields>
<QueryOptions />
上面是一段标准的查询caml,我发现去掉And节点,就像下面一样也可以查询出结果
<Query>
<Where>
<Contains>
<FieldRef Name='Title' />
<Value Type='Text'>1</Value>
</Contains>
<Leq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2010-1-1 00:00:00Z</Value>
</Leq>
<Geq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2009-1-4 00:00:00Z</Value>
</Geq>
</Where>
</Query>
<ViewFields>
<FieldRef Name='Title' />
</ViewFields>
<QueryOptions />
但是结果不是我想要的了,其实它光是查询符合前两个条件也就是<Contains> 和 <Leq>的记录,而不管后面的<Geq>条件了,几经比较,原来caml查询字符串中,两个条件必须用And包起来,多了一个呢,就要将前面的And和第三个条件组合一个And,以此类推,不知道大家明白我的意思没有
也就是说,两个条件的caml查询就像下面的
<Query>
<Where>
<Contains>
<FieldRef Name='Title' />
<Value Type='Text'>1</Value>
</Contains>
<Leq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2010-1-1 00:00:00Z</Value>
</Leq>
</Where>
</Query>
<ViewFields>
<FieldRef Name='Title' />
</ViewFields>
<QueryOptions />
但是三个条件的就要变成
<Where>
<And>
<And>
<Contains>
<FieldRef Name='Title' />
<Value Type='Text'>1</Value>
</Contains>
<Leq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2010-1-1 00:00:00Z</Value>
</Leq>
</And>
<Geq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2009-1-4 00:00:00Z</Value>
</Geq>
</And>
</Where>
</Query>
<ViewFields>
<FieldRef Name='Title' />
</ViewFields>
<QueryOptions />
上面是一段标准的查询caml,我发现去掉And节点,就像下面一样也可以查询出结果
<Query>
<Where>
<Contains>
<FieldRef Name='Title' />
<Value Type='Text'>1</Value>
</Contains>
<Leq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2010-1-1 00:00:00Z</Value>
</Leq>
<Geq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2009-1-4 00:00:00Z</Value>
</Geq>
</Where>
</Query>
<ViewFields>
<FieldRef Name='Title' />
</ViewFields>
<QueryOptions />
但是结果不是我想要的了,其实它光是查询符合前两个条件也就是<Contains> 和 <Leq>的记录,而不管后面的<Geq>条件了,几经比较,原来caml查询字符串中,两个条件必须用And包起来,多了一个呢,就要将前面的And和第三个条件组合一个And,以此类推,不知道大家明白我的意思没有
也就是说,两个条件的caml查询就像下面的
<Query>
<Where>
<Contains>
<FieldRef Name='Title' />
<Value Type='Text'>1</Value>
</Contains>
<Leq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2010-1-1 00:00:00Z</Value>
</Leq>
</Where>
</Query>
<ViewFields>
<FieldRef Name='Title' />
</ViewFields>
<QueryOptions />
但是三个条件的就要变成
<Query>
<Where>
<And>
<And>
<Contains>
<FieldRef Name='Title' />
<Value Type='Text'>1</Value>
</Contains>
<Leq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2010-1-1 00:00:00Z</Value>
</Leq>
</And>
<Geq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2009-1-4 00:00:00Z</Value>
</Geq>
</And>
</Where>
</Query>
<ViewFields>
<FieldRef Name='Title' />
</ViewFields>
<QueryOptions />
四个条件的就要变成
<Where>
<And>
<And>
<Contains>
<FieldRef Name='Title' />
<Value Type='Text'>1</Value>
</Contains>
<Leq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2010-1-1 00:00:00Z</Value>
</Leq>
</And>
<Geq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2009-1-4 00:00:00Z</Value>
</Geq>
</And>
</Where>
</Query>
<ViewFields>
<FieldRef Name='Title' />
</ViewFields>
<QueryOptions />
四个条件的就要变成
<Query>
<Where>
<And>
<And>
<And>
<Contains>
<FieldRef Name='Title' />
<Value Type='Text'>1</Value>
</Contains>
<Leq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2010-1-1 00:00:00Z</Value>
</Leq>
</And>
<Geq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2009-1-4 00:00:00Z</Value>
</Geq>
</And>
<Contains>
<FieldRef Name='Content'/>
<Value Type='Text'>1</Value>
</Contains>
</And>
</Where>
</Query>
<ViewFields>
<FieldRef Name='Title' />
</ViewFields>
<QueryOptions />
caml中的_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_是汉字的unicode编码,在程序中不能使用字段名字直接访问到字段,必须使用这个unicode,sharepoint只认这个,或者使用 SP_Web.Lists["已过期动态"].Fields["动态发布时间"].InternalName也可以,_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_就是“动态发布时间”这几个字的编码
SPWeb Web = SPControl.GetContextWeb(this.Context);
SPList List = Web.Lists["Zoo"];
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='xiaoxiong' /></OrderBy><Where><Eq><FieldRef Name='xiaogou'/><Value Type='Choice'>xiaohuang</Value></Eq></Where>";
query.ViewAttributes = "Scope='Recursive'"; //设置范围为递归,包含子文件夹
query.ViewFields = "<FieldRef Name='xiaoji' /><FieldRef Name='xiaomao' />";
SPListItemCollection unprocessedItems = List.GetItems(query);
DataTable dt1 = unprocessedItems.GetDataTable();
<Where>
<And>
<And>
<And>
<Contains>
<FieldRef Name='Title' />
<Value Type='Text'>1</Value>
</Contains>
<Leq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2010-1-1 00:00:00Z</Value>
</Leq>
</And>
<Geq>
<FieldRef Name='_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_' />
<Value Type='DateTime'>2009-1-4 00:00:00Z</Value>
</Geq>
</And>
<Contains>
<FieldRef Name='Content'/>
<Value Type='Text'>1</Value>
</Contains>
</And>
</Where>
</Query>
<ViewFields>
<FieldRef Name='Title' />
</ViewFields>
<QueryOptions />
caml中的_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_是汉字的unicode编码,在程序中不能使用字段名字直接访问到字段,必须使用这个unicode,sharepoint只认这个,或者使用 SP_Web.Lists["已过期动态"].Fields["动态发布时间"].InternalName也可以,_x52a8__x6001__x53d1__x5e03__x65f6__x95f4_就是“动态发布时间”这几个字的编码
SPWeb Web = SPControl.GetContextWeb(this.Context);
SPList List = Web.Lists["Zoo"];
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='xiaoxiong' /></OrderBy><Where><Eq><FieldRef Name='xiaogou'/><Value Type='Choice'>xiaohuang</Value></Eq></Where>";
query.ViewAttributes = "Scope='Recursive'"; //设置范围为递归,包含子文件夹
query.ViewFields = "<FieldRef Name='xiaoji' /><FieldRef Name='xiaomao' />";
SPListItemCollection unprocessedItems = List.GetItems(query);
DataTable dt1 = unprocessedItems.GetDataTable();
o ViewAttributes
a. Scope='Default' : 只顯示指定文件夾下的項目及子文件夾
b. Scope='FilesOnly' : 只顯示指定文件夾下的項目
c. Scope='Recursive' : 顯示所有項目,不顯示文件夾
d. Scope='RecursiveAll' : 顯示所有項目和所有子文件夾
a. Scope='Default' : 只顯示指定文件夾下的項目及子文件夾
b. Scope='FilesOnly' : 只顯示指定文件夾下的項目
c. Scope='Recursive' : 顯示所有項目,不顯示文件夾
d. Scope='RecursiveAll' : 顯示所有項目和所有子文件夾
o RowLimit
返回多少條記錄
返回多少條記錄
CAML语法-Query写法
元素 说明
And 并且
BeginsWith 以某字符串开始的
Contains 包含某字符串
Eq 等于
FieldRef 一个字段的引用 (在GroupBy 中使用)
Geq 大于等于
GroupBy 分组
Gt 大于
IsNotNull 非空
IsNull 空
Leq 小于等于
Lt 小于
Neq 不等于
Now 当前时间
Or 或
OrderBy 排序
Today 今天的日期
TodayIso 今天的日期(ISO格式)
Where Where子句
And 并且
BeginsWith 以某字符串开始的
Contains 包含某字符串
Eq 等于
FieldRef 一个字段的引用 (在GroupBy 中使用)
Geq 大于等于
GroupBy 分组
Gt 大于
IsNotNull 非空
IsNull 空
Leq 小于等于
Lt 小于
Neq 不等于
Now 当前时间
Or 或
OrderBy 排序
Today 今天的日期
TodayIso 今天的日期(ISO格式)
Where Where子句
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构