0

I am creating a hosted app in SharePoint. I want to retreive all the custom list from SharePoint and then from the custom lists I want to fetch column values. I got 2 columns with the names title and link. Link is a hyperlink column containing urls. My question is some of my custom lists doesnt have the column name "link" and some has. But I want to retreive those list that has that column. And title column is in every custom list. Some of the list has the column name "link" but with no values.

Error message: 0x800a138f - Runtime errors in JavaScript: Unable to retrieve the property get_url for a reference that is undefined or null

    function onSuccessItemFetch() {
    // Now you can access the array of SP.ListItemCollection
    for (var i = 0, length = listItemsCollection.length; i < length; i++) {
        var items = listItemsCollection[i];
        var enumerator = items.getEnumerator();

        while (enumerator.moveNext()) {
            var listItem = enumerator.get_current();
            if (typeof listItem.get_item('Link') != "undefined" && typeof listItem.get_item('Link') != null) {
            itemString += listItem.get_item('Title') + "&nbsp" + "&nbsp" + "&nbsp" + "&nbsp" + "&nbsp" + "&nbsp" + "<a href='" + listItem.get_item('Link').get_url() + "'>" + "öppna formulär" + "</a>" + "</br>";  //oListItem.get_item('Datum')
            $('#divAllaListItems').html(itemString);
            }
        }
    }
}

It works fine when I want to retrieve all the custom list titles and the title column values.

7
  • Will it allow you to check whether listItem.get_item('Link') is defined before you try to set itemString?
    – Erin L
    Commented Sep 30, 2015 at 20:02
  • If 1 list has not the column name link or has but with no value will it stop executing the code? why cant it just retreive items from list that has the column name "link"?
    – Sunreuz
    Commented Sep 30, 2015 at 20:05
  • You're trying to call the .get_url() method on an object that doesn't exist, so yes that should stop execution. So you should check whether the object exists before using it. So something like if(listItem.get_item('Link')) var linkURL = listItem.get_item('Link').get_url();
    – Erin L
    Commented Sep 30, 2015 at 20:10
  • I did like this what Eric Alexander showed me. ` while (enumerator.moveNext()) { var listItem = enumerator.get_current(); if (typeof listItem.get_item('Link') != "undefined" || typeof listItem.get_item('Link') != null) { itemString += listItem.get_item('Title') + " " + "<a href='" + listItem.get_item('Link').get_url() + "'>" + "open link" + "</a>" + "</br>"; //oListItem.get_item('Datum') $('#divAllaListItems').html(itemString); } }`
    – Sunreuz
    Commented Sep 30, 2015 at 20:16
  • But still I get the error message, maybe something is missing.
    – Sunreuz
    Commented Sep 30, 2015 at 20:17

1 Answer 1

0

You could do something like this:

while (enumerator.moveNext()) {
  var listItem = enumerator.get_current();
  if(typeof listItem.get_item('Link') != "undefined"){
    itemString += listItem.get_item('Title') +  " " + "<a href='" + listItem.get_item('Link').get_url() + "'>" + "Open link" + "</a>" + "</br>";
    $('#divAllListItems').html(itemString);
  }          
}

But it would probably be more efficient to filter those out in the first place with a CAML query, but we don't know what is happening with the rest of your script.

4
  • I tried with your code but I get same error message.
    – Sunreuz
    Commented Sep 30, 2015 at 20:13
  • @Eric you have undefined in quotes... is that correct?
    – Erin L
    Commented Sep 30, 2015 at 20:22
  • Yes, or else I have been testing for undefined things wrong my whole JS life. Commented Sep 30, 2015 at 20:34
  • So how can I solve this issue. Your code should be enough to filter out "undefined" and "null" values.
    – Sunreuz
    Commented Sep 30, 2015 at 20:48

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.