可以从共享点列表中获取所需的列吗?

时间:2012-05-15 07:16:39

标签: vb.net sharepoint caml

我正在使用以下Caml查询从共享点列表中获取数据。

    oSb.Append("         <OrderBy>")
    oSb.Append("              <FieldRef Name=""Title"" />")
    oSb.Append("         </OrderBy>")

但是这个查询给了我列表的所有列。 有没有办法使用Caml Query从共享点列表中获取所需的列? 请帮帮我。

1 个答案:

答案 0 :(得分:4)

查看有关“查看字段”的MSDN文章。您可以在运行CAML查询之前指定这些字段。

using (SPSite site = new SPSite("http://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"]);
           }
        }