1

I want to get data from a list. It contains some lookup fields. My problem is how to get lookup value from the list. I meant I want to expand lookup field values.

Here is the code I'm doing.

 var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                CamlQuery caml = CamlQuery.CreateAllItemsQuery(100);

                ListItemCollection libraryTagsItems = clientContext.Web.Lists.GetByTitle("Library Tags").GetItems(caml);
                var requestItems = clientContext.Web.Lists.GetByTitle("Requests").GetItems(caml);
                clientContext.Load(requestItems);
                clientContext.ExecuteQuery();
                var requestList = requestItems.Select(item => new   DashboardRequest()
                {
                    Id = Convert.ToInt32(item["ID"].ToString()),
                    BatchName = item["BatchName"].ToString(),
                    Facility = item["Facility"].ToString(),
                    Library = item["Library"],
                  }).ToList();
                  return requestList ;
            }

Library is a lookup field. Please tell me how to get the lookupvield value in a string format.

1

Try

var requestItems = clientContext.Web.Lists.GetByTitle("Test").GetItems(caml);
clientContext.Load(requestItems);
clientContext.ExecuteQuery();
List<DashboardRequest> _DashboardRequest = new List<DashboardRequest>();
foreach (var item in requestItems)
{
    _DashboardRequest.Add(new DashboardRequest()
    {
        Id = Convert.ToInt32(item["ID"].ToString()),
        BatchName = item["BatchName"].ToString(),
        Facility = item["Facility"].ToString(),
        Library = ((FieldLookupValue)item["Lk"]).LookupId.ToString()
    });
}
| improve this answer | |
  • thanks for your reply.It's working now. – SupunAravinda Jan 4 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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