Forum Discussion

MarkusLosco's avatar
MarkusLosco
Copper Contributor
Sep 17, 2020

Microsoft Graph Api Teams and Channels - filesFolder

Hello everyone,

we want to extend the script by Tommy Lee https://www.lee-ford.co.uk/backup-team so that the Team Channels can download the files and folders.

With the new Microsoft Graph Api Teams Beta I found the filesFolder see "https://graph.microsoft.com/beta/teams/$Teamsid/channels/$($channelId)/filesFolder". I would like to use this so that we (see script below) can download all files and folders or am I on the wrong way? 

 

 

 

 

 

 

            # List all items in drive
            $itemList = Invoke-GraphAPICall -URI "https://graph.microsoft.com/v1.0/groups/$($chosenTeam.id)/drive/list/items?`$expand=DriveItem"

            # Loop through items
            $itemList.value | ForEach-Object {

                $item = Invoke-GraphAPICall -URI "https://graph.microsoft.com/v1.0/groups/$($chosenTeam.id)/drive/items/$($_.DriveItem.id)"

                # If item can be downloaded
                if ($item."@microsoft.graph.downloadUrl") {

                    # Get path in relation to drive

                    $itemPath = $item.parentReference.path -replace "/drive/root:", ""
                    $fullFolderPath = "$Path/_Backup_Team_Temp_/Files/$itemPath" -replace "//", "/"
                    $fullPath = "$Path/_Backup_Team_Temp_/Files/$itemPath/$($item.name)" -replace "//", "/"

                    # Create folder to maintain structure
                    New-Item -ItemType Directory -Force -Path $fullFolderPath | Out-Null

                    # Download file
                    Write-Host "    - Saving $($item.name)... " -NoNewline
                    try {

                        Invoke-WebRequest -Uri $item."@microsoft.graph.downloadUrl" -OutFile $fullPath
                        Write-Host "SUCCESS" -ForegroundColor Green

                    }
                    catch {

                        Write-Host "FAILED" -ForegroundColor Red

                
                    }
            
                }

            }

        }
        else {

            Write-Host " - Excluding Files..."

        }

 

 

 

Resources