I am trying to upload a file to a sharepoint folder from C drive of my PC. But I am getting this error:
Exception calling "UploadFile" with "3" argument(s): "The remote server returned an error: (403) Forbidden." At C:\\Users\\Projects\\file_upload.ps1:18 char:1 + $webclient.UploadFile($destination +'/'+ $File.Name,'PUT', $File.Full ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException
The code is :
Add-Type -path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
Add-Type -path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
# Set the variables
$destination ='https://link to sharepoint site/foldername/'
$File =get-childitem 'C:\Users\path of the file in C drive'
# Since we’re doing this remotely, we need to authenticate
$securePasssword = ConvertTo-SecureString 'Password' -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential ('Username', $securePasssword)
# Upload the file
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = $credentials
$webclient.UploadFile($destination +'/'+ $File.Name,'PUT', $File.FullName)
If you could help me correct this code or suggest any other code which will work it would be a great help.
Try to use the script below:
$User = "user@Tenant.onmicrosoft.com"
$Password = '*******'
$SiteURL = "https://Tenant.sharepoint.com"
$Folder = "C:\Scripts\HpeQuota"
#Path where you want to Copy
$DocLibName = "Documents"
#Docs library
# 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\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, (ConvertTo-SecureString $Password -AsPlainText -Force))
$Context.Credentials = $Creds
#Retrieve list
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()
# Upload file
Foreach($File in (dir $Folder -File))
{
$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 = $File
$Upload = $List.RootFolder.Folders.GetByUrl("/Shared Documents/Cu folder").Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.