MOSS 查询问题

这个查询的caml我已经研究了一个礼拜了,可就是进展缓慢。

我的需求是一个不固定条件的查询,要组合四个不固定的条件,就是说可能是一个,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 />

但是三个条件的就要变成

<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 />

四个条件的就要变成

<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();
o    ViewAttributes
 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子句
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值