Question

to get SharePoint List dataI am having an issue accessing the REST server via the CSOM. I have tried this with both the CSOM and just using jQuery. Code examples and the associated errors below. Can anyone direct me to a working example or tell me what I am doing wrong?

This code is part of a SharePoint Hosted App and the list is just a list in the root web. The user has permission to access the list and the app.

CSOM Example: Yields: Fail! : App Web is not deployed for this app's request url http://mySharePointRootWebURL.local.

var data = new SP.RequestExecutor("http://mySharePointRootWebURL.local/");
data.executeAsync({
    method: "GET",
    headers: { "Accept": "application/json;odata=verbose" },
    url: "http://mySharePointRootWebURL.local/_api/web/lists/getbytitle(\'MyLstName\')/items",
    success: function (data) { console.log('success!'); },
    error: function (p1,p2,errorMessage) { console.log('Fail! :' + errorMessage); }
    });

I can see that this example is not hitting the root web at all (from the app / app web).


jQuery Example Yields: Resource interpreted as Script but transferred with MIME type text/plain: "http://mySharePointRootWebURL.local/_api/web/lists/getbytitle(\'MyLstName\')/items&…Query19104068602353800088_1379462071044&alt=json-in-script&_=1379462071045". jquery.js:9597 Uncaught SyntaxError: Unexpected token < items:1 fail! : Error: jQuery19104068602353800088_1379462071044 was not called

$.ajax({ 
    url: "http://mySharePointRootWebURL.local/_api/web/lists/getbytitle(\'MyListName\')/items",
    type: "GET",
    beforeSend: function(xhr){
    xhr.setRequestHeader('Accept', 'application/json;odata=verbose'); },
    headers: {"Accept":"application/json;odata=verbose"},
    success: function(data){ console.log("success"); },
    error: function errHandler(p1,p2,errMessage){ console.log("fail! : " + errMessage); },
    dataType: 'jsonp',
    crossDomain: true,
    data: {
        alt: 'json-in-script'
    },
});

This is working as far as accessing the REST server and returning data, the problem is that the headers are not being added at all (verified in Fiddler). Without the headers the data comes back in XML. If that's how it has to be I will work with it, I guess, but I'd prefer to get JSON.

Was it helpful?

Solution

Your code doesn't look right. Here's code that wors with the cross-domain library

   var executor = new SP.RequestExecutor(appweburl);

    executor.executeAsync(
    {
        url:
        appweburl +
        "/_api/SP.AppContextSite(@target)/web/lists/getByTitle('Contacts')/items" +
                        "?@target='" + hostweburl + "'" +
                        "&$select=Id,FirstName,Title,WorkPhone,Email" +
                        "&$orderby=Title,FirstName",
        method: "GET",
        headers: { "accept": "application/json;odata=verbose" },
        success: successHandler,
        error: errorHandler
    })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top