## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
Ricardo Dias
@slowdata
Andrew Koltyakov
@koltyakov
As a hint of the direction - https://github.com/s-KaiNet/spsave/blob/master/src/core/FileSaver.ts#L89 - you can take a look how to upload a file with sp-request.
Or using large files and streaming - https://github.com/koltyakov/pnp-upload
I'd encourage to dive into that samples and try it first.
Ricardo Dias
@slowdata
Andrew, I thank you very match. I'm new with this and struggling a lot to accomplish this.
Andrew Koltyakov
@koltyakov
All of the suggestions are based on node-sp-auth so you can easily use spsave as a black box and bind creds into there if diving to a low level and figuring out how SP REST API works is not an option.
Ricardo Dias
@slowdata
My main task is to upload a list of files for a Sharepoint List.
I need to accomplish this and if I do understand all... then perfect
Andrew Koltyakov
@koltyakov
Document library, not a list. List is a container for items not documents.
I'd just use spsave then as a black-box function for a purpose for uploading.
Ricardo Dias
@slowdata
My collegue that understands more of sharepoint than I says in list you get Picture field tha u can put there!
Ricardo Dias
@slowdata
It's an attachment!
Andrew Koltyakov
@koltyakov
The mechanics for uploading a file or attachments is mostly similar though.
Ricardo Dias
@slowdata
Andrew, once again thkx. I will look to pnp but I'm worry with authentication
Ricardo Dias
@slowdata
My head is exploding... Trying pnpjs and pnp-auth I just can´t pass the problems with Ecma Modules and Common
Andrew Koltyakov
@koltyakov
Ricardo Dias
@slowdata
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: L:\testes\sp-node\node_modules\@pnp\common\index.js
require() of ES modules is not supported.
require() of L:\testes\sp-node\node_modules\@pnp\common\index.js from L:\testes\sp-node\node_modules\pnp-auth\lib\NodeFetchClient.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
It's NodeFetchClient
Ricardo Dias
@slowdata
@koltyakov Since I made it using sp-request I'm trying to add an Item to the List. I think a get a problem because spr.post it's sending Patch and not a Post. Can I change that?
"X-HTTP-Method": "POST",
Ricardo Dias
@slowdata
Can someone help how to attach the file? Andrew? I made a new list item and now trying to attach the file.
Ricardo Dias
@slowdata
This is how I'm trying (followinf msft docs)
  const uri = "https://share.seg-social.pt/sites/iss_dpc/covid19_MedidasExcecionais/_api/lists/GetByTitle('BO_SSD_teste')/items({${id}})/AttachmentFiles/add(FileName='{test.csv}'); const r = await spr.post(uri, { body: f, headers: { "X-RequestDigest": digest, "X-HTTP-Method": "POST", "IF-MATCH": "*", "Content-Length": f.length, }, }); the file i get it from here: const f = await fs.readFile(path.resolve(__dirname, file)); * TypeError: Cannot read property 'indexOf' of null at isOnPremUrl (L:\testes\sharepoint-node\node_modules\node-sp-auth\lib\src\auth\IAuthOptions.js:6:17) Sergei Sergeev @s-KaiNet @slowdata you have an error in your url - const uri = "https://.... " is not needed, just const uri = https://... also why do you use extra {} in items({${id}})? You should use just tems(${id}) and for file name just FileName='test.csv') Ricardo Dias @slowdata @s-KaiNet , true. Thkx. Still have an error. 400 - {"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"pt-PT","value":"A expressão \"lists/GetByTitle('BO_SSD_teste')/items({35})/AttachmentFiles/add(FileName='{test.csv}')\" não é válida."}}} Somthing is not valid Sergei Sergeev @s-KaiNet you haven't fixed items and file name issues, please try fixing them Ricardo Dias @slowdata So the url is invalid you say? ok. Don't now where the problem is... Sergei Sergeev @s-KaiNet items({${id}}) should be items(\${id})
FileName='{test.csv}' should be FileName='test.csv'
Ricardo Dias
@slowdata
REally??
so dumb..
thkx Segei
Ricardo Dias
@slowdata
Sergei can you help me with a file problem. I uploaded a csv file and it was ok. Tried with a ZIP file and when i wen't to download it it doesn't open. How can i read the file so it is in the original state.
Sergei Sergeev
@s-KaiNet
it depends how you read a file. Something below should work:
var file = fs.readFileSync("path to the file")
spr.post(url, {
body: file,
.....
}
Ricardo Dias
@slowdata
const file = await fs.readFile(path.resolve(__dirname, fileName)
const r = await spr.post(uri, {
body: file,
"X-RequestDigest": digest,
"X-HTTP-Method": "POST",
"IF-MATCH": "*",
},
});
Sergei Sergeev
@s-KaiNet
strange... if you try any other binary file, say image, does it work?
Ricardo Dias
@slowdata
1m.. testing it
used png file. From 31k whent 105k and it opens with nothins
Windows 10 image vier says: It looks like we don't support this file format
Sergei Sergeev
@s-KaiNet
you use await fs.readFile - are you sure, that fs.readFile returns a promise?
Ricardo Dias
@slowdata
const fs = require("fs").promises;
should it be a stream and not a buffer?
Sergei Sergeev
@s-KaiNet
ah... ok
basically buffer should work
Ricardo Dias
@slowdata
I doesn't
it