0
votes

I'm working on a SharePoint add-in, whitch retrieves a file (and metadata) from a rest api and store the file in a SharePoint document library (with the name of the document). Requirement is that the add-in supports Classic as Modern sites. That's way SPFX is not an option.

I'm able to get the JSON from REST, but I'm a little stuck in my next steps.

The JSON format is straight forward:

{ 
size: 2399,
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
Data: "UEsDBBQAAAAIAIBxVFC38a4kqQAAAA4BAAATAAAAY3VzdG9tWG1sL2l0ZW0xLnhtbK2PwQE...............etc",
filename: "Invoice298393.docx"
}

I can save the file as Base64. My solution is similar to https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/upload-a-file-by-using-the-rest-api-and-jquery. This example retrieves it's data from a input contol and streams it data from there,....

I'll like to know how to get the json.Data and upload it to a SharePoint Document library.

Thanks in advance...

1

1 Answers

0
votes

Nevermind... Fixed it:

function b64EncodeUnicode(str) {
    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
        return String.fromCharCode('0x' + p1);
    }));
}
function b64DecodeUnicode(str) {
    return decodeURIComponent(Array.prototype.map.call(atob(str), function (c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));
}

    var jsonString = JSON.stringify(json.Data, null, 0);
    var base64EncodedArray = b64EncodeUnicode(jsonString);
    var content = new SP.Base64EncodedByteArray(base64EncodedArray); //base64 encoding