1

I've seen a lot of info on the above question but none of it has answered what I'm looking for, so far. I'm trying to retrieve list items that have a great number of lookup fields. I've had success getting to some of the lookup values but not others and I can't figure out why. Also, it would appear that 'null' items are not accepted in the returned data? If the lookup field does not contain a value, it throws an error? Below is my code.

The other issue (the big issue mentioned in my title) is that lookup values where I'm trying to pull in something, other than ID or Title, throws an error: reference not found or initialized. In this instance my lookup field is referencing a calc column returned as a single line of text (Course1Date). So my lookup field is Course1Date/DateRange, where DateRange represents the fieldname of the calc column. 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." I have posted this same question on stackoverflow in hopes of spreading the question. Apologies in advance if I'm not supposed to do that.

function getLookupValues(){
var clientContext = SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getByTitle('Lookup');
var caml = new SP.CamlQuery();
caml.set_viewXml('<View><Query><Where><Geq>' + 
            '<FieldRef Name=\'ID\'/><Value Type=\'Number\'>82</Value>' + 
            '</Geq></Where></Query><RowLimit>10</RowLimit></View>'); 

var returnedItems = list.getItems(caml);
clientContext.load(returnedItems);
clientContext.executeQueryAsync(success, failure);
}
function success(){
var listiteminfo='';
var enumerator = returnedItems.getEnumerator();
while(enumerator.moveNext())
{
  var listItem = enumerator.get_current();
  if(listItem.get_item('Course1')) {
    var course1 = listItem.get_item('Course1').get_lookupValue();
  }
  if(listItem.get_item('Course2')) {
    var course2 = listItem.get_item('Course2').get_lookupValue();
  }
  if(listItem.get_item('Course1Date')) {
    var course1Date = listItem.get_item('Course1Date').get_lookupValue();
  } //throws error for this lookup field

 listiteminfo += '\nCourse1: '+course1+'\nCourse2: '+course2+'\nCourse1Date: '+course1Date;

}
alert(listiteminfo);
        }
function failure(){
alert("Failed");
}
0

->1. Best way to deal with lookup field is always check if it contains value or it is null. Otherwise it will through exception if it contains null value.

Please refer below code -->

listItem.get_item('Course1') != null ? vallocation = objlocation.get_lookupValue() : "";

->2. You are receiving error at the below line -->

if(listItem.get_item('Course1Date')) {
    var course1Date = listItem.get_item('Course1Date').get_lookupValue();
  } //throws error for this lookup field

Here make sure that the internal name of the column(Course1Date) is same as you have mentioned.

Please make required changes and let me know if any issue.

  • Hey Rohit, I'm already doing that, if(listItem.get_item('Course1'), will give a true value if it is not blank. I get the issue when I try adding Course1Date. This is the only lookup field that is different from the rest. It looks to a calc column. – JackSki Mar 17 '17 at 15:29
0

I think you have course1 space date So space get converted into SharePoint indeed converts spaces to x0020 for internal names, because this is supposed to represent the url encode %20 (aka space). Because of this reason, you should prevent using spaces in field name (and staticname), as you've mentioned yourself. You can keep spaces in the display name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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