Access denied, SharePoint app. Copy file to another site collection

640 views Asked by At

UPDATE: I understand now that by installing an app in web scope, it only gets access to the hostweb and appweb. So I tried to batchinstall the app - and now the APP works. However, the App Part isnt available other than in the App Catalog. Does Anyone know a way to give the App Part permissions to other site collections, or batch install the App Part so its available in other places than app catalog?


have this code that I'm using for downloading a file from one sitecollection, and trying to upload it to another, in sharepoint online.

I'm getting a 403 that im not allowed to upload the file. The DOWNLOAD is fine. Does anyone have any clues?

var web;
var hostweburl;
var appweburl;

$(document).ready(function () {
    sharePointReady();
});

function sharePointReady() {
    hostweburl =
         decodeURIComponent(
             getQueryStringParameter('SPHostUrl')
     );
    appweburl =
        decodeURIComponent(
            getQueryStringParameter('SPAppWebUrl')
     );

    var scriptbase = hostweburl + '/_layouts/15/';

    $.getScript(scriptbase + 'SP.Runtime.js',
        function () {
            $.getScript(scriptbase + 'SP.js',
                function () { $.getScript(scriptbase + 'SP.RequestExecutor.js', copyAndUploadFile); }
            );
        }
    );
}

function getQueryStringParameter(param) {
    var params = document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == param) {
            return singleParam[1];
        }
    }
}

function copyAndUploadFile() {
    var targetUrl = "https://sogetiumea-my.sharepoint.com/personal/simonagren_sogetiumea_onmicrosoft_com";

    var executor = new SP.RequestExecutor(appweburl);

    var fileContentUrl = "_api/SP.AppContextSite(@target)/web/GetFileByServerRelativeUrl('/_catalogs/theme/15/fontscheme003.spfont')/$value?@target='" + hostweburl + "'";
    var fileTargetUrl = "_api/SP.AppContextSite(@target)/web/GetFolderByServerRelativeUrl('_catalogs/theme/15')/Files/Add(url='fontscheme003.spfont', overwrite=true)?@target='" + targetUrl + "'";




    $.ajax({
        url: "_api/contextinfo",
        type: "POST",
        contentType: "application/x-www-url-encoded",
        dataType: "json",
        headers: {
            "Accept": "application/json; odata=verbose",
        },
        success: function (data) {
            var digest = data.d.GetContextWebInformation.FormDigestValue;

            var getFileAction = {
                url: fileContentUrl,
                method: "GET",
                binaryStringResponseBody: true,
                success: function (getFileData) {
                    var results = data.body;

                    var copyFileAction = {
                        url: fileTargetUrl,
                        method: "POST",
                        headers: {
                            "Accept": "application/json;odata=verbose",
                            "X-RequestDigest": digest
                        },
                        contentType: "application/json;odata=vebose",
                        binaryStrinRequestBody: true,
                        body: getFileData.body,
                        success: function (copyFileData) {
                            alert("kopiering gick bra");
                        },
                        error: function (ex) {
                            alert(JSON.stringify(ex));
                        }

                    };
                    executor.executeAsync(copyFileAction);
                },
                error: function (ex) {
                    alert(JSON.stringify(ex));

                }
            };
            executor.executeAsync(getFileAction);
        },
        error: function (ex) {
            alert(JSON.stringify(ex));

        }
    });



}
1

There are 1 answers

0
Simon Ågren On

I used a workaround of sort.

  1. I added the app part to the app catalog, and used a hardcoded value to the mysite site collection (used as source for downloading/copying the file).
  2. Using developer tools I copied the html for the iframe used to show the app part i n the app catalog.
  3. I activated publishing on mysites root site collection.
  4. I added a scripteditor webpart. Then I added the copied iframe code. Voila, now the "app part" works.

    • At first the app listens to the current user, checks if a personal site exists, else it creates it.
    • Copies as cusom spcolor file from the mysite "_catalogs/theme/15" to same folder on the current users personal site.
    • Applies the spcolor file in "Applytheme".

This is based on the idea that Vesa has done with a provider hosted app!