Hello,
So finally managed to get it to work, both reading and writing files on sharepoint.
Here's how I did it in case someone finds this post in the future:
To read a file:
let url = "https://{site_url}/_api/web/GetFileByServerRelativeUrl('/Folder Name/{file_name}')/$value"
let header = {
"Authorization": "your_bearer_token",
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
};
let params = {
/* PUT YOUR HTTP PARAMS HERE */
};
let resultQuery = Resources["ContentLoaderFunctions"].LoadBinary(params);
// this is used to convert the binary result to a string, so not needed to save a file, but could be good if you need to get your data in the service.
var result = me.ConvertBlobToString({
Blob: resultQuery /* BLOB */
});
// this saves your file to a thingworx repository of your choice.
Things["YourFileRepository"].SaveBinary({
path: "/YourFileName.FileEnding" /* STRING */,
content: resultQuery /* BLOB */
});
To convert the binary returned from your "LoadBinary" function (me.ConvertBlobToString above):
let blobArray = [];
for (var i = 0; i < Blob.length; i++) blobArray.push(Blob[i]);
let string = base64EncodeBytes({
array: blobArray
});
var result = base64DecodeString(string);
Input type is of type "blob" and output is of type "string" for above service.
To create a file on your sharepoint platform:
let url = "https://{site_url}/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files/add(url='{file_name}',overwrite=true)";
let header = {
"Authorization": "your_bearer_token",
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
};
let params = {
/* PUT YOUR HTTP PARAMS HERE
header: header,
content: <This can be a string or come from a file on your file repository>
*/
};
/*
For excel files use PostBinary result will be a blob type:
result = Resources["ContentLoaderFunctions"].PostBinary(params);
you can then parse that into json:
result = me.ConvertBlobToString({
blob: resultQuery
});
result = JSON.parse(string);
For regular text files uses PostText
result will be a JSON
result = Resources["ContentLoaderFunctions"].PostText(params);
*/
Hope this helps someone else in case they are struggling with integration to sharepoint.