1

I can use Chrome Advanced REST tools to test out a get list items request on the host web

http://mydev/records/_api/Web/Lists/GetByTitle('Uber Documents')/Items?$select=Title

Ok now I want to use a similar call in SharePoint hosted app that makes a cross domain call to Host Web from Appp Web.

  var listName = "Uber Documents";
  executor.executeAsync(
    {

        url: appweburl +
       "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + listName + "')/items?@target='" +
             hostweburl + "'&$select=Title&$select=Target",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: SuccessHandler,
        error: errorHandler

I think my syntax is incorrect as the SuccessHandler fires but I get
Could not complete cross-domain call: Bad Request

Anybody got any tips on how you formulate the syntax.

2 Answers 2

1

It's unlikely that you have a field called 'Target' in any given list that you might call with this function...

It's probable that your first request in your original post worked just fine, while the second failed because the first didn't attempt to $select a field called Target

Something to try:

var executor = new SP.RequestExecutor(appweburl);
function execCrossDomainRequest(listTitle) {
    executor.executeAsync(
        {
            url:
                appweburl +
                "/_api/SP.AppContextSite(@target)/web/lists/getByTitle('" + listTitle + "')/items?$select=Title&@target='" +
                hostweburl + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: successHandler,
            error: errorHandler
        }
    );
}

function successHandler(data) {
    console.log(data);
    var jsonObject = JSON.parse(data.body),
        results = jsonObject.d.results;

    console.log(results);
}

function errorHandler(data, errorCode, errorMessage) {
    document.getElementById("renderAnnouncements").innerText =
        "Could not complete cross-domain call: " + errorMessage;
}

Then you can call it with execCrossDomainRequest('Uber Documents') and it should log the output into your console.

3
  • sorry I don't even get the failhandler triggered - will test now with my known good url Nov 17, 2014 at 18:14
  • Woops, I wasn't inserting the list title or requesting items... should be all fixed now..
    – John-M
    Nov 17, 2014 at 18:27
  • A few typos on my part but I have now tested on Doucments and Uber documents and passed both to renderAnnoucements - all good. Thanks very much. Time to go home! Daniel Nov 17, 2014 at 19:25
0

Try with this URL

EDIT

var url = appweburl +
               "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + listName + "')/items?@target='" + hostweburl + "'&$select=Title,Target"

$.ajax({    

url: url,
type: "GET",    
contentType: "application/json;odata=verbose",    
headers: {      "Accept": "application/json;odata=verbose" }, 
success: function (data) {      
        alert('Success')    
     },    
error: function (data) {      
        alert('Failed')    
    } 

});
5
  • @vipulKekar I am afraid no change in status - I added the trailing comma.Can you check your syntax. I can retest my handlers with a known good get web rest call if you need to verify my deployment situation. Nov 17, 2014 at 15:31
  • I hope you are creating executor as : var executor = new SP.RequestExecutor(appweburl); I have edited the answer. Can you post your entire code ? Nov 17, 2014 at 15:44
  • Yes creating var executor et. var url = appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + listName + "')/items?@target='" + hostweburl + "'&$select=Title,Target"; executor.executeAsync( { url: url, method: "GET", headers: { "Accept": "application/json; odata=verbose" }, success: function (data) { alert('Sucess') }, error: errorHandler } ); sorry about the formatting : daniel at westerdale.biz Nov 17, 2014 at 16:02
  • I change the url to var url2 = appweburl + "/_api/SP.AppContextSite(@target)/web/?@target='" + hostweburl + "'"; and get the success handler fires... so it has to be the other url . Note AppManifest permissions shows Web and List both set to read. I don't specify the list at build/publish time. Nov 17, 2014 at 16:30
  • I can send you my CrossDomainExec.js if need to verify my code - you can can get me on the above email or via twitter @ westerdaled. I am porting my app sharepoint hosted to on prem provider hosted so test on either. Daniel Nov 17, 2014 at 17:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.