Question

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.

Was it helpful?

Solution

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()
    });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top