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

I need to get a FieldLookup from a list (already queried). First line in the below code errors out "Unable to cast Field to FieldLookup". Yes, the field I am trying to retrieve is a lookup field only. Any help?
list - A sharepoint list
ctx - ClientContext instance

var fieldLookup = (FieldLookup)list.Fields.GetByInternalNameOrTitle("ALookupField");  
ctx.Load(fieldLookup, (s) => s.LookupList);  
ctx.ExecuteQuery(); 
share|improve this question

1 Answer 1

Found a hack to get the lookup list id.

    var field = list.Fields.GetByInternalNameOrTitle("ALookupField");  
    ctx.Load(field, (s) => s.SchemaXml);  
    ctx.ExecuteQuery();  
    var lookupListId = XElement.Parse(field.SchemaXml).Attributes().First(s => s.Name == "List").Value;  

Logically one would think that this List prop in schema corresponds to the list the field belongs to, but the fact is, it corresponds to the LookupList. That's MS way.

share|improve this answer

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.