Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 25064

SharePoint 2013 Online -REST SP.AppContextSite(@TargetSite) upload to another site collection - 403 Forbidden

$
0
0

I've got this jquery code I embedded into a page on one site and have no problem uploading to the document library on the same site collection.

trying to upload from a page on  sites/dev to Documents of sites/test

Getting Error: 403 Forbidden.  I've tried taking out the SP.AppContextSite(@TargetSite)/ code. No Luck either.

What am I missing something?

<label id="fileSelectorLabel">Select a file: </label><input id="fileSelectorInput" type="file"/><input class="InputButton" id="clearUploadFormButton" type="button" value="Reset"/><input class="InputButton" onclick="UploadFile();" id="uploadFileButton" type="button" value="Upload"/><script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"></script><script type="text/javascript">



function UploadFile() {

    alert("hi");


    // get the library name and file reference
    var fileInput = $('#fileSelectorInput');


    // 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);
    }
}

function ProcessUpload(fileInput) {
    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(fileInput.name, fileData);

    };
    reader.readAsArrayBuffer(fileInput);
}

function PerformUpload(fileName, fileData) {
    var url;
	

 	
        url = 'https://mytenant.sharepoint.com/sites/dev' +"/_api/SP.AppContextSite(@TargetSite)/web/lists/getByTitle(@TargetLibrary)/RootFolder/Files/add(url=@TargetFileName,overwrite='false')?"+"@TargetSite='" + 'https://mytenant.sharepoint.com/sites/test'+ "'" +"&@TargetLibrary='Documents'" +"&@TargetFileName='CV" + fileName + "'";
			alert(url);

    // use the request executor (cross domain library) to perform the upload

$.ajax({     
        url: url,
        type: "POST",
        headers: {"Accept": "application/json; odata=verbose","X-RequestDigest": $("#__REQUESTDIGEST").val()

        },
		data: JSON.stringify({ '__metadata': { 'type': 'SP.List' }, 'Title': 'New title' }),
		

        contentType: "application/json;odata=verbose",
        binaryStringRequestBody: true,
        body: fileData,
        success:successHandler,
        error: errorHandler
    });
}


     function successHandler(data) {
		 alert("success");

        $('#message').html("uploaded");     }

    // Function to handle the error event.
    // Prints the error message to the page.
    function errorHandler(data, errorCode, errorMessage) {
        $('#message').html("Unable to Upload Your file. Confirm you have not already uploaded for this Faculty.  Error: " + errorMessage + errorCode);
		alert("fail");
    }</script>




Viewing all articles
Browse latest Browse all 25064

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>