PrivateFunctions/Upload-SmallFileToSharePointOnline.ps1
<#
.SYNOPSIS This function uploads a small file up to 4MB in size to SharePoint Online. .DESCRIPTION This function uploads a small file up to 4MB in size to SharePoint Online. It makes use of the OneDrive "Upload large files" REST endpoint: https://docs.microsoft.com/en-us/OneDrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online #> function Upload-SmallFileToSharePointOnline { [CmdletBinding(PositionalBinding=$false)] [OutputType([Bool])] param ( # The path to the file on the local machine. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$filePath, # The path of the destination file in SharePoint Online. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$destinationFilePath, # The User Principal Name of the user whose SharePoint Online account will receive the file. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$siteUrl, # The Microsoft Graph authentication token. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$token, # Select the stream where the messages will be directed. [Parameter(Mandatory=$false)] [ValidateSet("Information", "Warning", "Error")] [String]$outputStream = "Error" ) # Verify that the file exists on the local machine if (!(Test-Path -Path $filePath -ErrorAction SilentlyContinue)) { Write-OutputMessage "The file '$($filePath)' on the local machine cannot be found." -OutputStream $outputStream -ReturnMessage:$false return $false } # Remove any "\" or "/" at the start of the SharePoint Online destination path if ($destinationFilePath.StartsWith("\")) { $destinationFilePath = $destinationFilePath.TrimStart("\") } if ($destinationFilePath.StartsWith("/")) { $destinationFilePath = $destinationFilePath.TrimStart("/") } # Verify that the siteUrl is valid if (!(Test-DomainValidity -Domain $siteUrl)) { Write-OutputMessage "The siteUrl '$($siteUrl)' is not valid." -OutputStream $outputStream -ReturnMessage:$false return $false } # Convert the file into ascii code $fileInBytes = [System.IO.File]::ReadAllBytes($filePath) $fileInAscii = [System.Text.Encoding]::ASCII.GetString($fileInBytes) # Upload the file to SharePoint Online Write-Information "Uploading the file '$($filePath)' to SharePoint Online." try { $invokeRestMethodParams = @{ Uri = "https://graph.microsoft.com/v1.0/sites/$($siteUrl)/drive/root:/$($destinationFilePath):/content" Method = "PUT" Headers = @{ Accept = "application/json" "Content-Type" = "text/plain" Authorization = "bearer $($token)" } Body = $fileInAscii } $response = Invoke-RestMethod @invokeRestMethodParams if (!$response -or [String]::IsNullOrWhiteSpace($response.id)) { Write-OutputMessage "Failed to upload the file '$($filePath)' to SharePoint Online." -OutputStream $outputStream -ReturnMessage:$false return $false } } catch { Write-OutputMessage "Exception occurred while uploading the file '$($filePath)' to SharePoint Online.`r`n$($_.Exception.Message)" -OutputStream $outputStream -ReturnMessage:$false return $false } # Successfully uploaded the file return $true } |