I can upload a file to a library in SharePoint Online. However, I get an error message as soon as I try to upload it inside a folder.
The error message is: Exception calling ExcuteQuery with 0 arguments: Access denied. You do not have permission to perform this action or access this ressource.
Line 1 below works well but not line 2.
1. $FileCreationInfo.URL = $File.Name
2. $FileCreationInfo.URL = "superfolder\" + $File.Name
It makes me crazy because it was a piece of code that already worked 6 months ago. Thanks for your help.
$sourceDocumentPath = "---" # Source document to spawn new documents from for the creation $User = "---" $webUrl = "---" $docLibraryName = "---" #Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" #Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll" $Password = Read-Host -Prompt "Please enter your password" -AsSecureString # ------------------------- #Open web and library #Bind to site collection $Context = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl) $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password) $Context.Credentials = $Creds #Retrieve list $uploadfolder = $Context.Web.Lists.GetByTitle($docLibraryName) $Context.Load($uploadfolder) $Context.ExecuteQuery() $File = get-item $sourceDocumentPath $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open) $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation $FileCreationInfo.Overwrite = $true $FileCreationInfo.ContentStream = $FileStream #$FileCreationInfo.URL = "superfolder\" + $File.Name $FileCreationInfo.URL = $File.Name write-host $FileCreationInfo.URL $Upload = $uploadfolder.RootFolder.Files.Add($FileCreationInfo) $Context.Load($Upload) $Context.ExecuteQuery() $FileStream.Close()