Last active
June 4, 2024 08:48
-
-
Save PokerGuy/5cdfc4ef146ce6b535bfd8e13469a2ab to your computer and use it in GitHub Desktop.
Push a file to SharePoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const push = async (file, fileName) => { | |
const client = new aws.SecretsManager(); | |
const spURL = `https://<your site>.sharepoint.com/sites/<your specific subsite>/_api/web/GetFolderByServerRelativeUrl('Documents')/Files/Add(url='${fileName}', overwrite=true)`; | |
try { | |
const data = await client.getSecretValue({ SecretId: '<whatever you called your secret>' }).promise(); | |
const secret = JSON.parse(data.SecretString).<whatever you called your secret>; | |
const getToken = await axios.post('https://accounts.accesscontrol.windows.net/<sharepoint resource id>/tokens/OAuth/2', | |
querystring.stringify({ | |
grant_type: 'client_credentials', | |
client_id: '<ask your sharepoint person for this it's a something@tenant id>', | |
client_secret: secret, | |
resource: '<ask your sharepoint person>' | |
}), { | |
headers: { | |
"Content-Type": "application/x-www-form-urlencoded" | |
} | |
} | |
) | |
const accessToken = getToken.data.access_token; | |
const getRequestDigest = await axios.post('https://<your site>.sharepoint.com/sites/<your specific subsite>/_api/contextinfo', {}, { | |
headers: { | |
"Authorization": `Bearer ${accessToken}`, | |
} | |
}) | |
const formDigestValue = getRequestDigest.data.FormDigestValue; | |
await axios.post(spURL, file, { | |
maxBodyLength: Infinity, | |
maxContentLength: Infinity, | |
headers: { | |
'Authorization': `Bearer ${token}`, | |
'X-RequestDigest': formDigestValue | |
} | |
}) | |
logger.info("Success"); | |
} catch (e) { | |
logger.error(e); | |
} | |
} | |
// If you are reading a file from a disk instead of having bytes like this example, you can replace the file in line 27 with something like Buffer.from(fs.readFileSync('./filename')) |
i don't know who you are but i really want to thank you for help you just saved me without even notice thanks again <3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This definitely helped get me in the right direction w/ axios; however, I think it might be simpler?, at least different in some cases like below.
At least it's not dealing with the
digest
detail which I don't fully understand; nor deal with the max*Length values.Hopefully that all makes sense. I've compiled the above code as drawn from a couple different abstracted objects and envvar constants as part of my projects code rather than a simple async anonymous function as the OP but hopefully it might help someone. I tried
got
at first but it got annoyingly complicated and axios turned out to be simpler.One note: Those
:/
and:/
portions of the URL do actually matter. My initial attempts with just GET or POST usinggot
and thenaxios
suggested they were artifacts of MS documentation but at least in this case they do matter.