Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

What's the best way to retrieve lookup values using ClientContext? I'm accessing a list using ClientContext (let's call it ParentList). That list contains several lookup fields (let's call one of them ChildId) pointing to other lists (call one of them ChildList). I'm currently using a ListItem to iterate over the results. Snippet:

ListItemCollection collListItem = oList.GetItems(camlQuery);

context.Load(collListItem,
    items => items.Include(
    item => item["title"],
    item => item["id"]));

context.ExecuteQuery();

foreach (ListItem oListItem in collListItem)
{           
    allTrials.Add(new Trial
    {
        Name = oListItem["title"].ToString(),
        Category = oListItem["id"].ToString(),
    });

}

I don't have anything regarding the lookup values in it yet because I haven't been able to wrap my head around that. I'm assuming I'll need to use "FieldLookupValue", but haven't used that before.

Thx!

share|improve this question
add comment

2 Answers

Retrieving a lookup field is no different than retrieving any other field, but getting at the value takes an extra step.

(using your example)

var childIdField = oListItem["ChildId"] as FieldLookupValue;

if (childIdField != null)
{
    var childId_Value = childIdField.LookupValue;
    var childId_Id = childIdField.LookupId;
}

The reason for the extra step is the field stores the value as both the Id and the Value, and if you use the .ToString() method on the field, it will return a string of the Id and Value with a ";#" seperator (if I remember correctly, I don't have it in front of me so the exact seperating string may be slightly different).

If the field is a multi-value lookup field, the result is an array of FieldLookupValues and the result will need to be cast and iteratted over to get individual values.

var childIdField = oListItem["ChildId"] as FieldLookupValue[];

if (childIdField != null)
{
    foreach(var lookupValue in childIdField)
    {
        var childId_Value = lookupValue.LookupValue;
        var childId_Id = lookupValue.LookupId;
    }
}

All code should be considered psuedo-code as I wrote it in the reply box. I wouldn't expect it to cut/paste and compile without being looked over for proper syntax -- but it should illustrate the steps needed to use lookup field values.

share|improve this answer
    
Yes that works, thank you so much for clearing that up, and your examples worked perfectly. below also works if youre sure there are values childIdField.LookupValue.ToString() Is there a shortcut for not having to use the var var1 ... as FieldLookupValue ? All my list items have 1 lookup value. –  user5244 Oct 14 '11 at 20:42
    
I don't know that it's a shortcut, but if you don't want the null check or the lookup id the value can be retrieved by placing the code inline; (oListItem["ChildId"] as FieldLookupValue).LookupValue. Of course, you can use a cast instead of as as well. –  David Culp Oct 16 '11 at 17:46
    
Also, to keep the site as useful as possible, once you are satisfied the question has been answered, be sure to mark your accepted answer. Thanks –  David Culp Oct 16 '11 at 17:48
add comment

the class SP.FieldLookupValue has two properties:

  • lookupid

  • lookupvalue

You can use the following sample code:

...
var itemsEnumerator = collListItem.getEnumerator();
while(itemsEnumerator.moveNext()){

    var currentItem = itemsEnumerator.get_current();
    var myId = currentItem.get_item('FIELD').**get_lookupId()**;
    var myTitle = currentItem.get_item('FIELD').**get_lookupValue()**;

    var result = 
        {
             Id: myId,
             Title: myTitle
         };
 };
...
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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