0

I'm using JavaScript to pull data from a SharePoint 2013 list. The REST request works fine and I can see the data in the debug windows of Chrome. I am struggling to determine how to access specific pieces of the data.

When I open the Chrome tools I can see the object and it looks like this {d:{...}}. I can expand it and look at the data.

What I have not been able to do is access specific elements say for example the Title column information for element 0. I've tried data.results[0].Title and other ways but nothing.

I can write the code to grab what I need just need to understand how to access the data in the code first.

Thanks for the help.

The REST call I'm using is as follows:

    var srcListName="MyList";
    var oDataUrl="https://somesharepoint.com/_api/web/lista/getbytitle('"+srcListName+"')/items?$;
            $.ajax({
        url:oDataUrl,
        type:"GET",
        dataType: "json",
        headers:{"accept":"application/json;odata=verbose"},
        success:mySuccHandler,
        error: myErrHandleer});

function mySuccHandler(data){
console.log(data);
}
function myErrorHandler(data,errMessage){

}

1 Answer 1

1

You are so close! You see it yourself in your console - you're just missing the d in your code!

function mySuccHandler(data){
    console.log(data);
    
    // after logging, you can see the "data" object is { d: {} },
    // so you need to include that single "d" property to access
    // anything inside :)

    var firstResultItemTitle = data.d.results[0].Title;

    // keep in mind though that if you are doing a query that
    // will (or might) return multiple items, you will get
    // a "results" array as a direct property of the "d" property

    var firstResultItem = data.d.results[0];
 
    // but if you do a query for a _single_ item, like
    // /_api/web/lists/getbytitle('My List')/items(3)  <- (only gets single item with ID 3)
    // the "d" property will not have a "results" array, it will be the 
    // single item itself, so you would access the item properties
    // directly

    var singleItemTitle = data.d.Title;
}

I think the disconnect might be that when you console.log(myVariable) and myVariable is an object, you (and I mean people in general) forget that the act of logging to the console does not preserve your variable name, it just gives you the "plain" object. So

var myVariable = {    // <-- is an object

    xxx: {            // <-- with a single property that is also an object
        something: [],
        somethingElse: 'abc'
    }

}

console.log(myVariable);

// will show up as

{xxx:{...}}

The other thing to keep in mind is that SharePoint's REST API is always going to return an object with a single, root property of d:

{
    d: {
        // other stuff
    }
}

You can't really change that, that's just what SharePoint gives you.

(I can't tell you how may code reviews I've been through with non-SharePoint developers and heard "what's up with that 'd' property? could that be named more descriptively?" :D )

2
  • I knew I was close and, as usual, after I post the question I find the answer I needed. Thanks for the assistance anyhow. Still figuring out how to access these objects...is there a guide somewhere you can point/direct me to that will provide this? Say, for example (using the above example) I wanted to retrieve the column headers from the same list. I guess the proper term is "Object Properties". Thanks. Commented Oct 21, 2020 at 18:16
  • For the column headers, you would have to query the list to get its fields, so that you can then check the display names of the fields. You won't be able to get anything but the internal names of the fields (as the property keys) if you are querying for the items in the list. Commented Oct 21, 2020 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.