Scheduled Task to Upload File to Sharepoint Online
Since SharePoint is not the just system you are using in your company, information technology might help to know how to upload documents to SharePoint from different systems programmatically. In this commodity, I desire to show you what you can upload all types of files to SharePoint and what to consider, when you want to upload documents to SharePoint with PowerShell.
Prerequisites
In order to upload documents to SharePoint programmatically, you have to ensure, that these prerequisites are met.
- PNP.PowerShell Module must be installed. For more than, read this commodity: Connect to SharePoint with PowerShell | SharePoint Online (sposcripts.com)
- You lot need to take owner rights for the library
- You need to allow custom site scripts for the site
Before you run the scripts
In the following script, I accept used a credential file, which you also should generate. Why? Because the purpose of this script, is to run in the background, without a credential prompt, so you lot can use information technology in a scheduled task. Don't forget to change the parameters to your respective scenario.
![Upload documents to SharePoint with PowerShell solution 2 Explanation of credential handling](https://sposcripts.com/wp-content/uploads/2021/10/image-14-1024x261.png)
PowerShell Scripts
I have provided a script to upload a single file and 1 to upload multiple files to the library.
Upload single certificate to SharePoint with PowerShell
With this script you can upload a unmarried document to SharePoint with a PowerShell script. The script is ready to be used for a scheduled job, but earlier you plan it, run it with your service account.
Before:
![Upload documents to SharePoint with PowerShell solution 3 SharePoint library before upload documents to SharePoint proceeded](https://sposcripts.com/wp-content/uploads/2021/10/image-8.png)
Param( $SiteUrl = "https://devmodernworkplace.sharepoint.com/sites/Sales/SubSite", $LibraryName = "Documents", $CredentialPath = "C:\Users\Serkar\Desktop\devmod.key", $FilePath = "C:\Users\Serkar\Desktop\Projectscope.xlsx" ) Function Consign-CredentialFile { param( $Username, $Path ) While ($Username -eq "" -or $null -eq $Username) { $Username = Read-Host "Please enter your username ([email protected])" } While ($Path -eq ""-or $nix -eq $Path) { $Path = Read-Host "Where should the credentials be exported to?" } $ParentPath = Divide-Path $Path If ((Test-Path $ParentPath) -eq $fake) { New-Item -ItemType Directory -Path $ParentPath } $Credential = Get-Credential($Username) $Credential | Export-Clixml -Path $Path Return $Credential } Function Import-CredentialFile ($Path) { if (! (Test-Path $Path)) { Write-Host "Could not discover the credential object at $Path. Delight export your credentials starting time" } else { Import-Clixml -Path $Path } } $Credential = Import-CredentialFile -Path $CredentialPath If ($Credential -eq $null) { $Username = Read-Host "Delight enter your username ([e-mail protected])" Export-CredentialFile -Path $CredentialPath -Username $Username $Credential = Import-CredentialFile $CredentialPath } #Connect to site Connect-PnPOnline -Url $SiteUrl -Credentials $Credential #upload documents to SharePoint Add-PnPFile -Path $FilePath -Folder $LibraryName
After:
![Upload documents to SharePoint with PowerShell solution 4 SharePoint library after upload documents to SharePoint proceeded](https://sposcripts.com/wp-content/uploads/2021/10/image-10.png)
Upload multiple documents to SharePoint with PowerShell
In order to upload multiple documents to SharePoint, I recommend putting the files in a dedicated folder, so the script can iterate through all documents in the folder.
![Upload documents to SharePoint with PowerShell solution 5 Screenshot of a folder, where all documents are located in](https://sposcripts.com/wp-content/uploads/2021/10/image-11.png)
Before:
![Upload documents to SharePoint with PowerShell solution 3 SharePoint library before upload documents to SharePoint proceeded](https://sposcripts.com/wp-content/uploads/2021/10/image-8.png)
Param( $SiteUrl = "https://devmodernworkplace.sharepoint.com/sites/Sales/SubSite", $LibraryName = "Shared Documents", $CredentialPath = "C:\Users\Serkar\Desktop\devmod.key", $FolderPath = "C:\Users\Serkar\Desktop\Projects" ) Role Export-CredentialFile { param( $Username, $Path ) While ($Username -eq "" -or $null -eq $Username) { $Username = Read-Host "Delight enter your username ([electronic mail protected])" } While ($Path -eq ""-or $null -eq $Path) { $Path = Read-Host "Where should the credentials be exported to?" } $ParentPath = Split-Path $Path If ((Test-Path $ParentPath) -eq $faux) { New-Item -ItemType Directory -Path $ParentPath } $Credential = Get-Credential($Username) $Credential | Export-Clixml -Path $Path Return $Credential } Office Import-CredentialFile ($Path) { if (! (Test-Path $Path)) { Write-Host "Could not observe the credential object at $Path. Please consign your credentials starting time" } else { Import-Clixml -Path $Path } } $Credential = Import-CredentialFile -Path $CredentialPath If ($Credential -eq $null) { $Username = Read-Host "Please enter your username ([email protected])" Export-CredentialFile -Path $CredentialPath -Username $Username $Credential = Import-CredentialFile $CredentialPath } #Connect to site Connect-PnPOnline -Url $SiteUrl -Credentials $Credential #upload documents to SharePoint Get-ChildItem -Path $FolderPath |ForEach-Object { Add-PnPFile -Path $_.FullName -Folder $LibraryName }
Subsequently:
![Upload documents to SharePoint with PowerShell solution 7 SharePoint library after upload documents to SharePoint proceeded](https://sposcripts.com/wp-content/uploads/2021/10/image-12.png)
Troubleshooting
Access denied
![Upload documents to SharePoint with PowerShell solution 8 Access denied error message](https://sposcripts.com/wp-content/uploads/2021/10/image-6.png)
Add-PnPFile -Path $FilePath -Folder $LibraryName Add together-PnPFile : Access denied. At line:i char:1 + Add-PnPFile -Path $FilePath -Folder $LibraryName + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], ServerUnauthorizedAccessException + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile
If yous experience this issue, custom scripts are not allowed on this site. You have to enable it by connecting to the tenant with a SharePoint Ambassador and enabling custom scripts:
$SiteUrl = "https://devmodernworkplace.sharepoint.com/sites/Sales" $TenantUrl = "https://devmodernworkplace-admin.sharepoint.com/" Connect-PnPOnline $TenantUrl -interactive Set-PnPTenantSite $SiteUrl -DenyAddAndCustomizePages:$false Disconnect-PnPOnline
If y'all desire to read more most custom site scripts, check out this commodity: Allow or forestall custom script – SharePoint in Microsoft 365 | Microsoft Docs
Source: https://sposcripts.com/upload-documents-to-sharepoint-with-powershell/
0 Response to "Scheduled Task to Upload File to Sharepoint Online"
Post a Comment