1

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.

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.