Tuesday, March 17, 2015

CSOM LookUp Field value retrieval

Here is a simple code snippet to retrieve the LookUp value from the List Item.
You need to casting the retrieved item as FieldLookupValue to process further.

 ClientContext ctx = new ClientContext("http://win-gc3f02d6lkq/sites/apps");
            var list = ctx.Web.Lists.GetByTitle("Cities");
            CamlQuery query = new CamlQuery();
            query.ViewXml ="";
            ListItemCollection items = list.GetItems(query);
            foreach(var item in items)
                var lookUpId = item ["Country"] as FieldLookupValue;
                Console.WriteLine(item["Title"].ToString() + "--" + lookUpId.LookupValue);
This will lists the city name along with country field that has been used as lookup column from the Countries Master List in the "Cities" list.