4

如何在使用客户端 OM 查询列表时动态生成/指定要加载的项目字段列表?

这可以使用 CAML 查询上的标签来完成,但这会加载额外的不需要的字段,从而使有效负载更大。见这里:http: //blogs.technet.com/b/speschka/archive/2009/11/01/using-the-sharepoint-2010-client-object-model-part-3.aspx

这是我正在使用的测试代码:

    ClientContext clientContext = new ClientContext("http://myserver/sites/mysite");
Web site = clientContext.Web;

List list = clientContext.Web.Lists.GetByTitle("MyList");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View Scope='RecursiveAll'><RowLimit>100</RowLimit></View>";
ListItemCollection listItems = list.GetItems(camlQuery);

clientContext.Load(listItems,
      items => items.ListItemCollectionPosition,
      items => items.Include(
              item => item["ID"],
              item => item["Title"]
              ));

clientContext.ExecuteQuery();

我想要做的是在运行时为 Include 方法生成 lambda 表达式。仍然没有运气。我尝试的每个解决方案都会给我错误“不支持查询表达式”。

4

3 回答 3

0

您可以使用稍后要查询的列创建特定视图,并在调用 GetItems 方法时使用该视图。

于 2013-04-18T16:13:26.093 回答
0

我会向您推荐免费的 CAMLDesigner ( http://sharepoint.biwug.be/SitePages/Caml_Designer.aspx ) - 您可以在那里创建您的 caml 并在一个不错的 GUI 中检查结果。

https://sharepoint.stackexchange.com/a/69172/5170

于 2013-05-28T06:59:48.963 回答
0

如果要通过 CamlQuery 动态指定要加载的字段,可以循环加载每个字段:

var array = new string[] { "ID", "Title" }; // define dynamically, this is just an example
foreach (var field in array)
{
    clientContext.Load(listItems, includes => includes.Include(i => i[field]));
}

生成的查询与一个 Load 方法中的多个 lambda 表达式完全相同。

clientContext.Load(listItems, includes => includes.Include(i => i["ID"], i => i["Title"]));

两者都生成查询:

<Query Id="#" ObjectPathId="#">
    <Query SelectAllProperties="false">
        <Properties />
    </Query>
    <ChildItemQuery SelectAllProperties="false">
        <Properties>
            <Property Name="ID" ScalarProperty="true" />
            <Property Name="Title" ScalarProperty="true" />
        </Properties>
    </ChildItemQuery>
</Query>
于 2017-05-29T09:54:41.443 回答