SPQuery.ViewFields 属性

获取或设置由查询返回的视图字段。

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public Property ViewFields As String
    Get
    Set
用法
Dim instance As SPQuery
Dim value As String

value = instance.ViewFields

instance.ViewFields = value
public string ViewFields { get; set; }

属性值

类型:System.String
一个字符串,包含在指定的视图字段的协作应用程序标记语言片段。

备注

ViewFields属性包含对应于协作应用程序标记语言 (CAML)的ViewFields元素的内部 XML 字符串。

Type属性是的Lookup和User的字段特别有用。否则,如果没有该特性,查询可能不会返回预期的结果。在某些情况下,查询可能会完全失败。

备注

有关如何使用语言集成查询 (LINQ) 查询来检索SharePoint Foundation中的列表项的信息,请参阅Managing Data with LINQ to SharePoint

示例

下面的示例是一个控制台应用程序,则使用查询从任务列表中获取的项的集合并指定与每个项返回的字段值。后获取的数据,应用程序将打印到控制台的报告。

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using site As SPSite = New SPSite("https://localhost")
         Using web As SPWeb = site.OpenWeb()

            ' Build a query.
            Dim query As SPQuery = New SPQuery()
            query.Query = String.Concat( _
                              "<Where><Eq>", _
                                 "<FieldRef Name='Status'/>", _
                                 "<Value Type='CHOICE'>Not Started</Value>", _
                              "</Eq></Where>", _
                              "<OrderBy>", _
                                 "<FieldRef Name='DueDate' Ascending='TRUE' />", _
                                 "<FieldRef Name='Priority' Ascending='TRUE' />", _
                              "</OrderBy>")

            query.ViewFields = String.Concat( _
                                   "<FieldRef Name='AssignedTo' />", _
                                   "<FieldRef Name='LinkTitle' />", _
                                   "<FieldRef Name='DueDate' />", _
                                   "<FieldRef Name='Priority' />")

            query.ViewFieldsOnly = True ' Fetch only the data that we need.

            ' Get data from a list.
            Dim listUrl As String = web.ServerRelativeUrl + "/lists/tasks"
            Dim list As SPList = web.GetList(listUrl)
            Dim items As SPListItemCollection = list.GetItems(query)

            ' Print a report header.
            Console.WriteLine("{0,-25}  {1,-20}  {2,-25}  {3}", _
                  "Assigned To", "Task", "Due Date", "Priority")

            ' Print the details.
            Dim item As SPListItem
            For Each item In items
               Console.WriteLine("{0,-25}  {1,-20}  {2,-25}  {3}", _
                     item("AssignedTo"), item("LinkTitle"), item("DueDate"), item("Priority"))
            Next

         End Using
      End Using
      Console.ReadLine()
   End Sub
End Module
using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("https://localhost"))
         {
            using (SPWeb web = site.OpenWeb())
            {
               // Build a query.
               SPQuery query = new SPQuery();
               query.Query = string.Concat(
                              "<Where><Eq>",
                                 "<FieldRef Name='Status'/>",
                                 "<Value Type='CHOICE'>Not Started</Value>",
                              "</Eq></Where>",
                              "<OrderBy>",
                                 "<FieldRef Name='DueDate' Ascending='TRUE' />",
                                 "<FieldRef Name=’Priority’ Ascending='TRUE' />", 
                              "</OrderBy>");                    

               query.ViewFields = string.Concat(
                                   "<FieldRef Name='AssignedTo' />",
                                   "<FieldRef Name='LinkTitle' />",
                                   "<FieldRef Name='DueDate' />",
                                   "<FieldRef Name='Priority' />");

               query.ViewFieldsOnly = true; // Fetch only the data that we need.

               // Get data from a list.
               string listUrl = web.ServerRelativeUrl + "/lists/tasks";
               SPList list = web.GetList(listUrl);
               SPListItemCollection items = list.GetItems(query);

               // Print a report header.
               Console.WriteLine("{0,-25}  {1,-20}  {2,-25}  {3}",
                  "Assigned To", "Task", "Due Date", "Priority");

               // Print the details.
               foreach (SPListItem item in items)
               {
                  Console.WriteLine("{0,-25}  {1,-20}  {2,-25}  {3}",
                     item["AssignedTo"], item["LinkTitle"], item["DueDate"], item["Priority"]);
               }
            }
         }
         Console.ReadLine();
      }
   }
}

另请参阅

引用

SPQuery 类

SPQuery 成员

Microsoft.SharePoint 命名空间

ViewFieldsOnly