SharePoint列表SPList运用SPQuery进行查询的两个实用方法

SharePoint列表SPList运用SPQuery进行查询的两个实用方法

1.SPSite、SPWeb、SPList和SPQuery说明

1>SPSite

SPSite对象,SharePoint网站集对象,网站集包含一个顶层网站和顶层网站下所有子网站。

2>SPWeb

SPWeb对象,SharePoint网站对象,获取SharePoint网站内容,首先需要获取内容所在的网站SPWeb对象。

3>SPList

SPList对象,SharePoint列表对象,可通过SPList对象获取SharePoint网站列表内容。

4>SPQuery

SPQuery对象模型用于构建查询条件以查询列表数据,返回列表项集合。

2.SPQuery查询方法一

使用CAML查询语句进行查询,更多CAML查询语句请移步常用CAML查询语法及举例
查询网站列表中栏目Gender为男且栏目Class为一班的所有项,最后打印他们的身高

using(SPSite site = new SPSite("http://sharepointwebsite.com/Site"){
   using (SPWeb web = site.OpenWeb())
   {
      SPList list = web.GetList("/Lists/List");
      SPQuery query = new SPQuery();
      query.Query = @"<Where><And><Eq><FieldRef Name="Name" /><Value  Type="Text">小明</Value></Eq><Eq><FieldRef Name="Class" /><Value Type="Text">一班</Value></Eq></And></Where>";
      SPListItemCollection queryItems = list.GetItems(query);
      foreach(SPListItem item in queryItems)
      {
         Console.WriteLine(string.Format("一班男生{0}的身高为{1}",item.Name,item.Height))
      }
   }
}

2.SPQuery查询方法二

声明CAML对象使用Add_Filter进行查询
题目同上

using(SPSite site = new SPSite("http://sharepointwebsite.com/Site"){
   using (SPWeb web = site.OpenWeb())
   {
      SPList list = web.GetList("/Lists/List"); 
      CAML caml = new CAML();
      caml.Add_Filter("Add","Eq","Gender","Text","男",true);
      caml.Add_Filter("Add","Eq","Class","Text","一班",true);
      SPQuery query = new SPQuery();
      query.Query = caml.Where;
      SPListItemCollection queryItems = list.GetItems(query);
      foreach(SPListItem item in queryItems)
      {
         Console.WriteLine(string.Format("一班男生{0}的身高为{1}",item.Name,item.Height))
      }
   }
}

注意:Gender和Class栏目在列表必须存在且非空,否则运行代码会报错

参考

以上为个人整理总结的知识,如有遗漏或错误欢迎留言指出、点评,如要引用,请写引用说明,谢谢。
[1]: 刘中正, 王兴, 张志宇. SharePoint 2013应用开发实践[M]. 清华大学出版社, 2016.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值