Použití SPQuery k dotazování na seznam v SharePoint Serveru 2013

Původní číslo KB: 2755129

Tento článek popisuje, jak použít SPQuery k dotazování na seznam v Microsoft SharePoint Serveru 2013.

Použití SPQuery v prostředí SharePoint Management Shell

Pokud chcete použít SPQuery v prostředí SharePoint Management Shell, projděte si následující ukázkový skript:

$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()

Použití SPQuery v aplikacích C#

Pokud chcete použít SPQuery v aplikacích jazyka C#, projděte si následující příklad kódu:

  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);
        }
        }

Identifikace typu informací

Chcete-li identifikovat typ informací ve sloupci v seznamu, postupujte takto:

  1. Přihlaste se k webu Služby SharePoint pomocí přihlašovacích údajů správce pro kolekci webů.
  2. Otevřete seznam, například Úkol.
  3. V nabídce Seznam klikněte na Nastavení seznamu.
  4. Zobrazte typ informací v části Sloupec .