Home > database >  SP 2013 - Trying to get items from a list - some say undefined
SP 2013 - Trying to get items from a list - some say undefined

Time:11-04

I have a SharePoint 2013 Document Library which has some images stored in a column called "Name".

I am trying to get the URL of the image but it keeps saying "undefined". I also try to get another column "MyCol" (single line of text) and it says undefined. I can get other information from the Document Library (such as JobTitle - which is a Single Line of Text)

Any ideas?

EDIT: Further digging has shown me that when I do

console.log(JSON.stringify(data.d.results[result]));

It doesn't seem to pull back the MyCol value?

Thanks P

<script>

$.ajax ({
    
    url: _spPageContextInfo.webAbsoluteUrl "/_api/web/lists/getbytitle('photos')/items?expand=Name",

    dataType: 'json',

    async: false,

    headers: { "Accept": "application/json;odata=verbose" },

    success: function(data) {

        var html = '';

        var count = 0;

        for(var result in data.d.results) {


                html ='Name:'   data.d.results[result].Name   '<br>'; //this gives undefined
                html ='MyCol:'   data.d.results[result].MyCol   '<br>'; //this gives undefined
    
                html ='Job Title:'   data.d.results[result].JobTitle   '<br><br>'; //this gets the data from the Document Library

            count  ;

 

            if(count >= 6)

            {

                break;

            }

        }

        $('#staffInformation').append(html);

    },

    error: function ajaxError(response){

        console.log(response.status   ' '   response.statusText);

    }

});

</script>

CodePudding user response:

$expand is used to also grab data from a related item (typically Lookup or user field).

What you want is $select to specify columns to return. Use FileLeafRef to get the file name (also take a look at FileRef or FileDirRef :

url: _spPageContextInfo.webAbsoluteUrl "/_api/web/lists/getbytitle('photos')/items?$select=FileLeafRef"

Ref: Use OData query operations in SharePoint REST requests.

As a side note, you can reduce network footprint by changing the headers:

"Accept": "application/json;odata=nometadata"

The response will only contains the data, not the metadata. Just adjust the returning object property walking (there's no intermediate d)

  • Related