0

I am trying to make cross domain calls and use REST API in SharePoint provider hosted app but to no luck. Below are few things i tried.

I have tried implementing the following sample code to validate SP.RequestExecutor.js :

function CrossDomainAccess() {
    var executor = new SP.RequestExecutor(appWebUrl);
    var newHostWeb = "http://server/sites/your_other_sitecollection/";
    var url = appWebUrl + "/_api/SP.AppContextSite(@target)/web?" + "@target='" + targetSiteUrl + "'";

    executor.executeAsync({
        url: url,
        method: "GET",
        crossDomain: true,
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            alert(data.body);
            // var jsonObject = JSON.parse(data.body);
        },
        error: function (data, errorCode, errorMessage) {
            alert(errorMessage);
        }
    });
}

I get an error message saying App Web is not deployed for this app's request URL <appWebUrl> I tried this code from my local machine. Thinking it might be some issue triggering from localhost, I deployed the IIS. It didn't work there as well (Rest of the app functionality works fine, in C#).

I also tried to upload the file using REST API, that too failed with same error.

var appWebUrl = "";
var targetSiteUrl = "";
var digest = "";
var ready = false;

// extract the parameters from the page url (appweburl and hosturl
$(document).ready(function () {
    appWebUrl   = document.getElementById('hdn_spHostUrl').value;// 
    targetSiteUrl = "";

    // store the digest value for easy use later
    digest = "0xD2CD0EC5C7CEF0E2611DC53D493850ECEDFE9AE83DBE2685D11495275BC0F2A2455057EDB7AC98FBB1882BC0728303E9F0A8E6F227C08C378A710A79CA6BD0C2,18 Feb 2019 11:41:37 -0000";//$("#__REQUESTDIGEST").val();

    // load the request executor script, once ready set a flag that can be used to check against later
    $.getScript(appWebUrl + "/_layouts/15/SP.RequestExecutor.js", function () {
        ready = true;
    });
});


function UploadFile() {
    // if we have not finished loading scripts then display an alert
    if (!ready) {
        alert("Oooooops... Please wait for the page to finish loading before attempting to upload a file");
        return;
    }

    // get the library name and file reference
    var docLibrary = "Shared Documents";//$('#docLibraryNameInput').val();
    var fileInput = $('#uploadfile');

    // if there is no document library name alert the user and return
    if (!docLibrary || docLibrary == '') {
        alert("Oooooops... It doesn't look like you entered the name of the library you would like to upload your file to");
        return;
    }

    // if we couldnt get a reference to the file input then alert the user and return
    if (!fileInput || fileInput.length != 1) {
        alert('Oooooops... An error occured processing your input.');
        return;
    }
    else if (!fileInput[0].files) {
        alert("Oooooops... It doesn't look like your browse supports uploading files in this way");
        return;
    }
    else if (fileInput[0].files.length <= 0) {
        alert("Oooooops it doesn't look like you selected a file. Please select a file and try again.");
        return;
    }

    // for each file in the list of files process the upload
    for (var i = 0; i < fileInput[0].files.length; i++) {
        var file = fileInput[0].files[i];
        ProcessUpload(file, docLibrary, '');
    }
}

function ProcessUpload(fileInput, docLibraryName, folderName) {
    var reader = new FileReader();
    reader.onload = function (result) {
        var fileName = '',
         libraryName = '',
         fileData = '';

        var byteArray = new Uint8Array(result.target.result)
        for (var i = 0; i < byteArray.byteLength; i++) {
            fileData += String.fromCharCode(byteArray[i])
        }

        // once we have the file perform the actual upload
        PerformUpload(docLibraryName, fileInput.name, folderName, fileData);

    };
    reader.readAsArrayBuffer(fileInput);
}

function PerformUpload(libraryName, fileName, folderName, fileData) {
    var url;

    // if there is no folder name then just upload to the root folder
    if (folderName == "") {
        url = appWebUrl + "/_api/SP.AppContextSite(@TargetSite)/web/lists/getByTitle(@TargetLibrary)/RootFolder/Files/add(url=@TargetFileName,overwrite='true')?" +
            "@TargetSite='" + targetSiteUrl + "'" +
            "&@TargetLibrary='" + libraryName + "'" +
            "&@TargetFileName='" + fileName + "'";
    }
    else {
        // if there is a folder name then upload into this folder
        url = appWebUrl + "/_api/SP.AppContextSite(@TargetSite)/web/lists/getByTitle(@TargetLibrary)/RootFolder/folders(@TargetFolderName)/files/add(url=@TargetFileName,overwrite='true')?" +
           "@TargetSite='" + targetSiteUrl + "'" +
           "&@TargetLibrary='" + libraryName + "'" +
           "&@TargetFolderName='" + folderName + "'" +
           "&@TargetFileName='" + fileName + "'";
    }

    // use the request executor (cross domain library) to perform the upload
    var reqExecutor = new SP.RequestExecutor(appWebUrl);
    reqExecutor.executeAsync({
        url: url,
        method: "POST",
        headers: {
            "Accept": "application/json; odata=verbose",
            "X-RequestDigest": digest
        },
        contentType: "application/json;odata=verbose",
        binaryStringRequestBody: true,
        body: fileData,
        success: function (x, y, z) {
            alert("Success! Your file was uploaded to SharePoint.");
        },
        error: function (x, y, z) {
            alert("Oooooops... it looks like something went wrong uploading your file.");
        }
    });
}

Am I missing any configuration? Below are the app properties ;

    <App xmlns="http://schemas.microsoft.com/sharepoint/2012/app/manifest"
     Name="AmatA3Solution"
     ProductID="{7b69eebe-7138-498e-9e22-caa94286dd52}"
     Version="1.0.0.9"
     SharePointMinVersion="15.0.0.0"
>
  <Properties>
    <Title>Amat.A3Solution</Title>
    <StartPage>~remoteAppUrl/Pages/Default.aspx?{StandardTokens}</StartPage>
  </Properties>

  <AppPrincipal>
    <RemoteWebApplication ClientId="*" />
  </AppPrincipal>
  <AppPermissionRequests>
    <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="Read" />
    <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="Read" />
    <AppPermissionRequest Scope="http://sharepoint/social/tenant" Right="Read" />
  </AppPermissionRequests>
</App>

My SP site is: https://SharePointURL.com which I set as appWebUrl and the IIS is in: https://IISUrl.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.