0

I am trying to get values of few lookup columns and person or group column of my list using the below code

var context = SP.ClientContext.get_current();
var web=context.get_web(); 
var list=web.get_lists(); 
var targetList;
targetList = list.getByTitle("list name");
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Text'>"+myID+"</Value></Eq></Where></Query></View>");
window.collListItem = targetList.getItems(camlQuery);
context.load(collListItem);              
context.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded),Function.createDelegate(this, onQueryFailed));
function onQuerySucceeded(sender,args)
{

 var listItemEnumerator = collListItem.getEnumerator();
 while (listItemEnumerator.moveNext()) {
 var oListItem = listItemEnumerator.get_current();
 var a =  oListItem.get_item('lookup column').get_lookupValue();
 var b = oListItem.get_item('person or group').get_lookupValue();
}
 function onQueryFailed(sender,args){alert('failed');}

But it throws error

The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

How to get the value of those columns?

1 Answer 1

0

You need to add the lookup column in the <ViewFields> section! like this :

<View><ViewFields><FieldRef Name='" + lookupFieldName + "' LookupId='TRUE' /></ViewFields></View>
2
  • Thanks. That worked! but why doesn't SharePoint fetch the lookup values & person or group values? Looks like the above query will fetch only the columns that were given in FieldRef. What if I need to get the values of all columns(lookups & person or group inclusive) of a list item? Do i have to mention all those columns under FieldRef?
    – Obiliraj
    Commented May 9, 2016 at 7:32
  • Once you use the ViewFields, you will be getting what you ask for. If you need anything you can use undefined CAML Query (dont't specify a query - According to Microsoft). I would suggest using this tool that will help you build your query better spcamlviewer.codeplex.com Good Luck! Commented May 24, 2016 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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