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')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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