0

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

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

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

4

1 回答 1

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"]);
           }
        }
于 2012-05-15T07:22:13.440 回答