sharepoint的caml查询语句的小发现 Virus

这个查询的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子句
 

【Blog】http://virusswb.cnblogs.com/

【MSN】jorden008@hotmail.com

【说明】转载请标明出处,谢谢

反馈文章质量,你可以通过快速通道评论:

原文地址:https://www.cnblogs.com/virusswb/p/1368071.html