从提供商托管应用获取发布网页上的sharepoint内容字段的值

时间:2016-09-07 17:34:31

标签: sharepoint sharepoint-2013 sharepoint-clientobject

在使用C#和客户端对象模型托管的SharePoint加载项提供程序中,我尝试在发布页面上获取sharepoint内容字段的值。

我先试了一下:

List list = clientContext.Web.Lists.GetByTitle("Pages");
ListItem item = list.GetItemById(7020);
clientContext.Load(list);
clientContext.Load(item, p => p.FieldValues["QP_Question"]);
clientContext.ExecuteQuery();
ViewBag.Question = item.FieldValues["QP_Question"];

我收到了这个错误:

Microsoft.SharePoint.Client.InvalidQueryExpressionException
The query expression 'p.ListItem.FieldValues.get_Item("QP_Question")' is not supported.

然后我尝试了:

List list = clientContext.Web.Lists.GetByTitle("Pages");
ListItem item = list.GetItemById(7020);
PublishingPage pp = PublishingPage.GetPublishingPage(clientContext, item);
clientContext.Load(list);
clientContext.Load(item);
clientContext.Load(pp, p => p.ListItem.FieldValues["QP_Question"]);
clientContext.ExecuteQuery();
ViewBag.Question = pp.ListItem.FieldValues["QP_Question"];

仍然是同样的错误。

1 个答案:

答案 0 :(得分:0)

正确的代码是

List list = clientContext.Web.Lists.GetByTitle("Pages");
ListItem item = list.GetItemById(7020);
clientContext.Load(list);
clientContext.Load(item, p => p["QP_Question"]);
clientContext.ExecuteQuery();
ViewBag.Question = Convert.ToString(item["QP_Question"]);

p.["QP_Question"]代替p.FieldValues["QP_Question"]

要查询多个内容字段,请执行以下操作:

clientContext.Load(item, p => p["QP_Question"], p => p["QP_Answer"]);