Search code examples
sharepoint-2013caml

CAML Query Null Hyperlink Field Causes list to not appear


My list has two hyperlink fields, all new items will have both fields filled but some older items that I have to add do not have both. First, I added a few of the more recent items that have both hyperlinks to test that my CAML Query worked, and it did. Until I added an item that did not have both hyperlink fields filled in.

I've tried adding spaces to the field, does not work.

My query:

var hclientContext = new SP.ClientContext.get_current();
var hList = hclientContext.get_web().get_lists().getByTitle('HRReportsList');
// New caml for getting list based on view
var camlQueryHR = new SP.CamlQuery();
camlQueryHR.set_viewXml('<View Scope="RecursiveAll"><Query><Where><And><Eq><FieldRef Name="Year"/>' +
'<Value Type="Text">2019</Value></Eq><Eq><FieldRef Name="Report_x0020_Type"/>' +
'<Value Type="Choice">Turnover</Value></Eq></And></Where>'  +
'<OrderBy><FieldRef Name="YYYYMM" Ascending="False" /></OrderBy>' +
'</Query><RowLimit>50</RowLimit><QueryOptions>' +
'<ViewAttributes Scope="Recursive" /></QueryOptions></View>');
this.hcollListItem = hList.getItems(camlQueryHR);
// end of caml for list view
//this.ncollListItem = nList.getItems("");
hclientContext.load(hcollListItem);
hclientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}

to get items I have some HTML, for example:

    hListItem.get_item('InfographicLink').get_description() + '</a></br></br>' +
'<a href="' + hListItem.get_item('ReportLink').get_url() + '" target="_blank">' +
    hListItem.get_item('ReportLink').get_description() + '</a></div>';

Error in the console is: 'Unable to get property 'get_url' of undefined or null reference'

Not a surprise, I just don't know how to get it to work knowing that I need to allow some hyperlinks to be empty.


Solution

  • Add a check to these two hyperlink fields, make sure they are not null like this:

    while (listItemEnumerator.moveNext()) 
    {
            var hListItem = listItemEnumerator.get_current();
    
            if(hListItem.get_item("ReportLink") && hListItem.get_item("InfographicLink"))
            {
                listItemInfo +=  hListItem.get_item('InfographicLink').get_description() + '</a></br></br>' +
             '<a href="' + hListItem.get_item('ReportLink').get_url() + '" target="_blank">' +
               hListItem.get_item('ReportLink').get_description() + '</a></div>';
            }
    }
    

    Here is a similar question for your reference:

    JSOM get_url of image null or undefined