-
Download PHP for Windows: https://windows.php.net/downloads/releases/php-8.2.6-Win32-vs16-x64.zip
-
Unzip in your Program Files folder: (Unblock the zip before unzipping. Right Click, "Properties" to unblock)
C:\Program Files\php-8-x64\
-
Download the
cacert.pem
and save it in your PHP installation folder, ssl folder: https://curl.se/docs/caextract.htmlC:\Program Files\php-8-x64\extras\ssl
Add the following settings (change acording to your settings) in your php.ini (
C:\Program Files\php-8-x64\php.ini
)curl.cainfo = "C:\Program Files\php-8-x64\extras\ssl\cacert.pem" openssl.cafile = "C:\Program Files\php-8-x64\extras\ssl\cacert.pem"
-
Add the PHP installation folder into your PATH Environment Variables
-
Install PHP Composer with Windows Installer: https://getcomposer.org/download/
It will detect your PHP installation and add the proper Environment Variables for you. If not, you can add as describe on the previous step
-
Launch Visual Studio Code, in the terminal, run the following command for installing all dependencies:
$>cd .\uploadFile_to_sp\ $>composer update
(Need admin rights on Azure AD)
- Go to the Azure Active Directory portal: Azure AD Portal
- Click Add > App registration
- Name: SharePoint access granter
- Supported account types: Accounts in this organizational directory only (TENANT_NAME only - Single tenant)
- Redirect URI (Optional): Select "Web" platform and keep the input text tempty
- Click "Register"
- After the Application Registration has been created, mark down:
- Application (client) ID
- Directory (tenant) ID
- Go to the "API Permissions" menu and click "Add a permission"
- Select "Microsoft Graph"
- Select "Application permissions"
- Search for "Sites.FullControl.All" and Add that permission
- Repeat steps 7 to 10 but select "SharePoint" on step 8 instead of "Microsoft Graph"
- After adding these 2 permissions, you can "Grant admin consent for TENANT_NAME" otherwise it won't work
- You can remove the permission "User.Read"
- Create the Application Client ID Secret through the (side) menu "Certificates & secrets"
- Select the tab "Client secrets"
- Click "New client secret"
- Add a description like "secret for granter"
- Expires: pick a period and click "Add"
- Mark down the secret value
Important: We will only use that Application (Client ID) registration for authorizing the second Application Client ID (below) to access to a specific SharePoint site as we do not want to provide FullControl to the SharePoint SiteCollection
Create the "Application SharePoint uploader access" Application Registration (Application Client ID)
Same steps like "Create the "Application SharePoint uploader access" Application Registration (Application Client ID)" except that you need to pick the API permission "Sites.Selected" instead of "Sites.FullControl.All":
- Click Add > App registration
- Name: Application SharePoint uploader access
- Supported account types: Accounts in this organizational directory only (TENANT_NAME only - Single tenant)
- Redirect URI (Optional): Select "Web" platform and keep the input text tempty
- Click "Register"
- After the Application Registration has been created, mark down:
- Application (client) ID
- Directory (tenant) ID
- Go to the "API Permissions" menu and click "Add a permission"
- Select "Microsoft Graph"
- Select "Application permissions"
- Search for "Sites.Selected" and Add that permission
- Repeat steps 7 to 10 but select "SharePoint" on step 8 instead of "Microsoft Graph"
- After adding these 2 permissions, you can "Grant admin consent for TENANT_NAME" otherwise it won't work
- You can remove the permission "User.Read"
- Create the Application Client ID Secret through the (side) menu "Certificates & secrets"
- Select the tab "Client secrets"
- Click "New client secret"
- Add a description like "secret for app sharepoint uploader"
- Expires: pick a period and click "Add"
- Mark down the secret value and add it in the ".env" file
-
Go to your SharePoint website home page URL where you want to upload your files
Example: https://SHAREPOINT_NAME/sites/MySP
-
Add the following text to the URL "/_api/site/id". The final URL will look like this:
Example: https://SHAREPOINT_NAME/sites/MySP/_api/site/id
-
Mark down your SharePoint "Site ID" from the XML output and add it in the ".env" file
- Fill the file ".env.granter" using the information created from the step "SharePoint access Granter" and "SharePoint Site ID"
- Run the PHP script "granter_appid_to_sharepoint.php"
- Your Application Client ID dedicated for uploading file in SharePoint is now allowed to upload (write) file in the SharePoint Site ID (Document Library) specified in your ".env.granter". You can go to the next step
- Fill the file ".env" using the information created from the step "Application SharePoint uploader access" and "SharePoint Site ID"
- Edit the PHP variable named "$file_fullpath_to_upload" (line ~13) in the PHP script "upload_file_to_sharepoint.php" to specify the file that you want to upload
- Run the PHP script "upload_file_to_sharepoint.php"
- You will see your uploaded file in the SharePoint
Note: The script will automatically generate the folder tree structure from the SharePoint Document Library root folder. Please check the PHP script "upload_file_to_sharepoint.php" for more details ("$new_folder_path" and "$new_folder_name" line ~51)
- Edit the script
list_files_from_sharepoint.php
to change the variable$folder_to_list
value according to the folder that you want to list the content - Run the script
list_files_from_sharepoint.php
- You will see all the folders / files from your the value specify in the variable
$folder_to_list