In this short tutorial, you'll learn how to upload a file to Sharepoint in a programmatic way. We will be using Microsoft Graph API for that.
Our sample code contained in this repo is based on python, but since we are basically calling Microsoft Graph API URLs you should be able to easily translate it to your language of choice.
First, go to Azure Active Directory and create a new app registration. The name does not matter, and neither do all the parameters. The only two things you have to do are:
- create a
client_secret
and note it down. We will put it intoCLIENT_SECRET
shell variable, along with ourCLIENT_ID
variable; also note down your Azure Active Directory Tenant ID and put it in a variable (AAD_TENANT_ID
) - grant to the app registration the following API permissions under
Microsoft Graph
(see also image below):Sites.ReadWrite.All
Remember to Grant admin consent for your directory after you have added permissions.
Just a word of caution: you are basically allowing your service principal to be able to read and write on any Sharepoint site of your organization. Be aware of this.
Now, go to your Office 365 Sharepoint tenant and create a new Sharepoint website. Take note of the following parameters:
- the name of your Office 365 tenant (
M365_TENANT_NAME
) - the name of the Sharepoint site you have created (
SHP_SITE_NAME
)
and put them inside the proper shell variables.
That's it. Run:
python3 shp.py
and in the following prompt take note of the file_url
displayed, like in this image:
Connect to a browser, and you should be able to see a dummy PDF file uploaded.