I'm trying to get the url from an Image/Hyperlink field in a SP2013 List. My query works until I add in the request for Image data.
Unable to get property 'get_url' of undefined or null reference
I've followed other examples and still can't seem to fix the issue. My problem is that not all of the items in the list will have an image, so I want to try and validate this before I do anything with the information.
var siteUrl = '/blog/';
function retrieveListItems() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Posts');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\' Ascending="False"/>' +
'<Value Type=\'Number\'>1</Value></Geq></Where><OrderBy><FieldRef Name =\'Modified\' Ascending="False"/></Query><RowLimit>10</RowLimit></OrderBy></View>');
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem, 'Include(Id, Title, Summary, Image)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
if(oListItem.get_item("Image").get_url()){
listItemInfo += '\nID: ' + oListItem.get_id() +
'\nTitle: ' + oListItem.get_item('Title') +
'\nSummary: ' + oListItem.get_item('Summary') +
'\nImage url: '+ oListItem.get_item('Image').get_url();
}
else{
listItemInfo += '\nID: ' + oListItem.get_id() +
'\nTitle: ' + oListItem.get_item('Title') +
'\nSummary: ' + oListItem.get_item('Summary');
}
}
alert(listItemInfo.toString());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}