Microsoft Graph API and PowerShell provide a way to upload files to SharePoint using Office 365, providing an alternative means of uploading large amounts of data while still utilizing the power of online file storage.
The “upload file to sharepoint using graph api c#” is a tutorial that will show you how to upload files to SharePoint with the MS Graph API and PowerShell.
Using PowerShell to upload files to OneDrive or SharePoint using the Microsoft Graph API
I’ll show you how to upload files to SharePoint sites, including child folders, in the lesson below.
There is no way to accomplish this directly using the Microsoft Graph API URLs in the lesson. For your convenience, I’ve included a cmdlet in a module.
Achieve you want to know how you can do this without a module? Then go to the final paragraph and look at the official Microsoft Graph API documentation links.
Let’s go to work on Optimized. In PowerShell, mga
Using the Microsoft Graph API to get the Authorization token
Powershell is now open!
You may use the following cmdlet to install the module and submodule:
-Scope CurrentUser Install-Module Optimized.Mga.SharePoint
Copy the cmdlet below. Fill in the right values for ApplicationID, ClientSecret, and Tenant.
By invoking the cmdlet, you will generate an Authorization token that will be used by other cmdlets (such as Get-Mga) in the backend.
-ClientSecret ‘XXXX’ -ApplicationID ‘b5954443-ad10-4d1c-8cbc-dc05268a1858’ -Tenant ‘bwit.onmicrosoft.com’ $null = Connect-Mga -ClientSecret ‘XXXX’ -ApplicationID ‘b5954443-ad10-4d1c-8c
You should have gotten a notification saying that you are logged in if everything went fine.
We simply need one cmdlet this time: To upload files, use Upload-MgaSharePointFiles.
Upload files to SharePoint sites and OneDrives using Upload-MgaSharePointFiles. The cmdlet is included as part of the module. It takes the following value as a parameter:
- TenantName
- Site
- ItemPath / Item
- ChildFolders
TenantName and Site are self-explanatory. The TenantName is the address ‘*.onmicrosoft.com’. The Site field specifies the URL of the site to which you wish to upload a file.
The Item or ItemPath argument differs somewhat. The path string to the item you wish to upload to SharePoint is sent to ItemPath.
You may pick ‘Copy as path’ when you Shift + Right click a file, as seen in the image below. “C:tempblatemp.txt” will be the path for the copy.
How to use the MS Graph API and PowerShell to upload files to SharePoint
-ItemPath “C:tempblatemp.txt” -TenantName “m365x794103.onmicrosoft.com” -Site “XXXX” Upload-MgaSharePointFiles -ItemPath “C:tempblatemp.txt” -TenantName “m365x794103.onmicrosoft.com”
The argument Item takes an item from the [System.IO.FileSystemInfo] object. That is, before you start Upload-MgaSharePointFiles, you must first run Get-Item -Path C:tempblatemp.txt.
-Item Upload-MgaSharePointFiles (Get-Item “C:tempblatemp.txt”) -Site ‘XXXX’ -TenantName’m365x794103.onmicrosoft.com’ createdBy: @application=; user=; createdBy: @application=; user=; createdBy: @application=; user=; Date: 2021-12-15T17:03:27ZZZZZZZZZZZZZZZZZZZZZZZ id: 01MIYRG5I55BYRTHLDVZEJQTJWXXQDVKWP eTag: “1971E81D-639D-48AE-984D-36BDE03AAACF,7” eTag: “1971E81D-639D-48AE-984D-36BDE03AAACF,7” eTag: “1971E81D-639D-48AE-984D-36BDE03A lastModifiedBy: @application=; lastModifiedBy: @application=; lastModifiedBy: @application=; lastModifiedBy: @application=; last Date: 2021-12-15T18:27:32ZZZZZZZZZZZZZZZZZZZZZZZ blatemp.txt is a text file with the name blatemp.txt.
It all depends on how Upload-MgaSharePointFiles will be used. Do you wish to upload things in bulk? I’d recommend starting with Get-Item / Get-ChildItem and then using a foreach loop.
Get-ChildItem -Path’C:temp’foreach ($item in $Items) $Items = Get-ChildItem -Path’C:temp’foreach ($item in $Items) $Items = Get-ChildItem – -Item Upload-MgaSharePointFiles $item -Site ‘XXXX’ -TenantName ‘XXXX’
There is a non-mandatory parameter called ChildFolders. If you don’t provide ChildFolders, the root is utilized instead.
An array of subfolders may be sent to the ChildFolder option. You may use the following command to upload a file to a subfolder:
-Item Upload-MgaSharePointFiles (Get-Item “C:tempblatemp.txt”) -ChildFolders ‘XXXX’,’Personnel files’,’bas.wijdenes’,’Personal’ -TenantName ‘XXXX’,’Personal’,’Personal’,’Personal’,’Personal’,’Personal’,’Personal’,’Personal’,’Personal’,’Personal’,’Personal’ @application=; user=; createdBy: @application=; user=; createdBy: @application=; user=; createdB Date: 2021-12-15T17:03:27ZZZZZZZZZZZZZZZZZZZZZZZZZZZ “1971E81D-639D-48AE-984D-36BDE03AAACF,7” is an eTag. 01MIYRG5I55BYRTHLDVZEJQTJWXXQDVKWP id: 01MIYRG5I55BYRTHLDVZEJQTJWXXQDVKWP id: 01MIYRG5I55 @application=; user=; lastModifiedBy: 2021-12-15T18:27:32Z lastModifiedDateTime blatemp.txt is a text file with the name blatemp.txt.
As seen in the picture below, ChildFolders are folders in the root.
How to use the MS Graph API and PowerShell to upload files to SharePoint
The Microsoft Graph API documentation
This lesson makes use of the Microsoft Graph API documentation:
Get a SharePoint site: Microsoft Graph beta | Microsoft Docs | Get a SharePoint site
Microsoft Docs | Resumable file upload: Resumable file upload – Microsoft Graph v1.0
Related
The “powershell post file to api” is a PowerShell command that can be used to upload files to SharePoint using the MS Graph API.
Related Tags
- upload folder to sharepoint online powershell ms graph
- upload file in powershell
- powershell upload file to box
- invoke-webrequest upload binary file
- box com powershell