Domanda

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){

}
È stato utile?

Soluzione

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 )

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top