使用 SPQuery 查询 SharePoint Server 2013 中的列表

原始 KB 编号: 2755129

本文介绍如何使用 SPQuery 在 Microsoft SharePoint Server 2013 中查询列表。

在 SharePoint 命令行管理程序中使用 SPQuery

若要在 SharePoint 命令行管理程序中使用 SPQuery,请参阅以下示例脚本:

$web=Get-SPWeb http://sps15/sites/SiteName #This URL is the URL of Your SharePoint Site
$list=$web.Lists["Task"] #"Task" is the name of the list.
$query=New-Object Microsoft.SharePoint.SPQuery
$query.Query = "<where><eq><FieldRef Name='status'/><value Type='CHOICE'>Not Started</value></eq></where>"

# 'Status'is the name of the List Column. 'CHOICE'is the information type of the Column.

$SPListItemCollection = $list.GetItems($query)
$SPListItemCollection.Count
$SPListItemCollection | select Web, DisplayName
$web.Dispose()
$list=$web.Lists["Task"] 
$query=New-Object Microsoft.SharePoint.SPQuery
$query.Query="<Where><Eq><FieldRef Name='Age'/><Value Type='Number'>400</Value></Eq></Where>" 
$SPListItemCollection = $list.GetItems($query)
$SPListItemCollection.Count
$SPListItemCollection | select Web, DisplayName
$web.Dispose()

在 C# 应用程序中使用 SPQuery

若要在 C# 应用程序中使用 SPQuery,请参阅以下示例代码:

  static void Main(string[] args)
        {
        SPSite cursite=new SPSite("http://sps15/sites/new");//This URL is the URL of your SharePoint Site.
        SPWeb curweb = cursite.OpenWeb();
        SPQuery curQry = new SPQuery();
        curQry.Query = "<where><eq><FieldRef name='status'/><value type='CHOICE'>Not Started</value></ep></where>";
        SPList mylist = curweb.Lists["Task"];//Task is the name for the list.
          SPListItemCollection curitems = mylist.GetItems(curQry);
          foreach (SPListItem curitem in curitems)
          {
              string resultitem = curitem["Title"].ToString();
              Console.Write(resultitem+"\n\r");
          }
        if (curweb != null)
        {
            string title = curweb.Title;
            Console.Write(title);
        }
        }

确定信息类型

若要标识列表中列中信息的信息类型,请执行以下步骤:

  1. 使用网站集的管理员凭据登录到 SharePoint 网站。
  2. 打开 任务等列表
  3. 在“ 列表 ”菜单上,单击“ 列表设置”。
  4. “列” 部分中查看信息类型。