Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Error Uploading file in folder PowerShell

$
0
0

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()


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>