How to download files from SharePoint with MS Graph API and PowerShell
The SharePoint team has released a new way to access the files stored in their site without having to use WebDAV. This is done through Graph API and PowerShell, two richly featured tools that are part of Microsoft Office 365.
The “microsoft graph api get item-id” is a command line tool that allows users to download files from SharePoint. The “Microsoft Graph API” is a Microsoft’s web service that provides access to data in the cloud.
Download SharePoint files from a site to your local device.
I demonstrate how to request all files from SharePoint sites, including child folders, in the lesson below. You may also get them right now by downloading them.
There is no way to accomplish this directly using the Microsoft Graph API URLs in the lesson.
For this, I’ve built two cmdlets in a module that you may utilize.
Accomplish you want to learn how to do it without using 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.
First, let’s look into Get-MgaSharePointFiles.
Get-MgaSharePointFiles looks for the relevant site, as well as any child folders.
There are three parameters, the first of which, ChildFolders, I’ll go over in greater detail below. The other two are simpler to comprehend: TenantName and Site.
TenantName is the name of your tenancy (With or without .onmicrosoft.com). The name of the SharePoint site is Site.
You may obtain all files from a site’s root directory using the following command:
-TenantName’m365x794103.onmicrosoft.com’ -Site ‘XXXX’ produced using Get-MgaSharePointFiles Date: 2021-06-07T10:15:34ZZZZZZZZZZZZZZZZZZZZZZZZ id: 01MIYRG5LAJSJGNXYNRVA2A2XWKCQZ66C6 eTag: “66924C60-0DDF-418D-A06A-F650A19F785E,1” eTag: “66924C60-0DDF-418D-A06A-F650A19F785E,1” eTag: “66924C60-0DDF-418D-A06A- lastModified Date: 2021-06-07T10:15:34ZZZZZZZZZZZZZZZZZZZZZZZZ XXXXXXXXXXXXXXXXXXXX
There is a non-mandatory parameter called ChildFolders. If you don’t provide ChildFolders, the root is utilized instead.
As seen in the picture below, ChildFolders are folders in the root.
How to use the MS Graph API and PowerShell to get files from SharePoint
An array of subfolders may be sent to the ChildFolder option. So, for example, if you wish to search for files in the above-mentioned directory Personal, use the following command:
Get-MgaSharePointFiles -TenantName ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -Site ‘XXXX’ -ChildFolders ‘XXXX’,’Personnel files’,’bas.wijdenes’,’Personal’,’Personal’,’Personal’,’Personal’,’Personal’,’Personal’,’Personal’,’Personal’,’Personal’,’Personal’,’Personal 2021-06-07T14:00:21Z “81017A49-4536-418C-9A71-0A03F46CD0CD,5” eTag: “81017A49-4536-418C-9A71-0A03F46CD0CD,5” id: 01MIYRG5KJPIAYCNSFRRAZU4IKAP2GZUGN lastModified Date: 2021-06-07T14:01:18ZZZZZZZZZZZZZZZZZZZZZZZZ Document.docx is the name of the document.docx file.
All objects in the directory are returned by Get-MgaSharePointFiles. Included in this are the ChildFolders.
This is because you’ll constantly see a list of everything. The drawback is that you will need to filter.
When you use Get-MgaSharePointFiles and save the results in the variable $SPItems, you can use Where-Object to filter through them.
$SPItems = Get-MgaSharePointFiles -TenantName ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Where-Object $_.Name -like “*Document.docx” $SPItem = $SPItems | Where-Object $_.Name -like “*Document.docx”
We now have the proper file in $SPItem and can begin downloading it.
Let’s utilize PowerShell to get a file from SharePoint using Download-MgaSharePointFiles.
Because Download-MgaSharePointFiles is used in conjunction with Get-MgaSharePointFiles, it needs an object from Get-MgaSharePointFiles.
That’s why the fact that we populated the variable $SPItem in the previous chapter comes in handy.
SPItem and OutputFolder are two parameters for Download-MgaSharePointFiles.
The whole object $SPItem is required by SPItem.
OutputFolder is self-explanatory. This is the location where the file will be saved when it has been downloaded.
You may use the following command to download a file from SharePoint:
-SPItem $Item -OutputFolder ‘C:temp’ Download-MgaSharePointFiles
The name of the file will always be the same as it is in SharePoint.
Do you want to get all of the files? The $SPItems may then be used in a foreach loop as follows:
$Item in $SPItems) foreach ‘Download-MgaSharePointFiles -SPItem $Item -OutputFolder ‘C:temp’ ‘Download-MgaSharePointFiles -SPItem $Item -OutputFolder ‘C:temp’ ‘Download-MgaSharePointFiles
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
List a folder’s contents: List a folder’s contents – Microsoft Graph v1.0 | Microsoft Docs
Download the following file: Microsoft Graph v1.0 | Microsoft Docs | Download
Watch This Video-
The “graph explorer” is a PowerShell script that allows users to download files from SharePoint with the MS Graph API. The script requires the .NET Framework 4.0, and was created by Microsoft MVP Adam Driscoll. The script can be downloaded on GitHub here: https://github.com/adamdriscoll/graph-explorer
- microsoft graph api download attachment
- microsoft graph api drives
- microsoft graph api download sharepoint file c#
- upload file to sharepoint document library using graph api
- microsoft graph api onedrive example c#