0

From a SharePoint Add-in, provider hosted, using C# and the client object model, I am trying to get the value of a sharepoint content field on a publishing page.

I tried first:

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"];

I received this error:

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

Then I tried:

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"];

Still the same error.

Cyril
  • 134
  • 1
  • 4
  • 15

1 Answers1

0

The correct code is

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"]);

It is p.["QP_Question"] instead of p.FieldValues["QP_Question"]

To query more than one content field, do like that:

clientContext.Load(item, p => p["QP_Question"], p => p["QP_Answer"]);
Cyril
  • 134
  • 1
  • 4
  • 15