TransWikia.com

OneDrive for Business - Upload document using PowerShell

SharePoint Asked by J.dock on August 31, 2020

I am trying to upload a file (actually copy a file from a local machine) to folder in OneDrive for Business

Unfortunately I have not been able to find any PowerShell examples that can upload a file to OneDrive.

Is it even possible?

$User = "[email protected]" 
$SiteURL = "https://domaincorp.sharepoint.com/sites/GED-Domain/Shared%20Documents/Forms/AllItems.aspx?cid=59d99667%2D45be%2D4ad9%2D8df8%2D569b7ce5bfeb&RootFolder=%2Fsites%2FGED%2DDomain%2DIT%2FShared%20Documents%2F04%5FINFRASTRUCTURE%2FBackup&FolderCTID=0x0120003A5DFAA3F20FCA48BBF44305D69A465B"
$Folder = "C:UsersSysAdminBackup"
#DocDocLibName is document libary name 
$DocLibName = "Backup"
$foldername = "tes"
#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll"
Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Runtime.dll"

$Password  = ConvertTo-SecureString ‘StrongPassword’ -AsPlainText -Force

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)

$Context.Credentials = $Creds

#Retrieve list
$List = $Context.Web.Lists.GetByTitle("$DocLibName")
$Context.Load($List)
$Context.Load($List.RootFolder)
$Context.ExecuteQuery()
$ServerRelativeUrlOfRootFolder = $List.RootFolder.ServerRelativeUrl
$uploadFolderUrl=  $ServerRelativeUrlOfRootFolder+"/"+$foldername

#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
 if($foldername -eq $null)
  {
  $Upload = $List.RootFolder.Files.Add($FileCreationInfo)
  }
  Else
  {
   $targetFolder = $Context.Web.GetFolderByServerRelativeUrl($uploadFolderUrl)
   $Upload = $targetFolder.Files.Add($FileCreationInfo);
  }
#$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}

The error is :

Exception lors de l'appel de «ExecuteQuery» avec «0» argument(s): «Cannot contact site at the specified URL https://domaincorp.sharepoint.com/sites/GED-Domain/Shared%20Documents/Forms/AllI
tems.aspx?cid=59d99667%2D45be%2D4ad9%2D8df8%2D569b7ce5bfeb&RootFolder=%2Fsites%2FGED%2Ddomain%2DIT%2FShared%20Documents%2F04%5FINFRASTRUCTURE%2FBackup&FolderCTID=0x0120003A5DFAA3F20FCA48BBF443
05D69A465B.»
Au caractère C:UsersSysAdminDesktopscript.ps1:23 : 1
+ $Context.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ClientRequestException

2 Answers

$UserName="BLABLABLA"
$pwd ="StrongPassword"
$SiteUrl = "https://domaincorp-my.sharepoint.com/personal/user_domain_fr/"
$encpassword = convertto-securestring -String $pwd -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $encpassword
Connect-PnPOnline -Url $SiteUrl -Credentials $cred
Add-PnPFile -Path .localFileName.docx -Folder 'Shared DocumentsBackup'

The error is :

Add-PnPFile : Access denied. You do not have permission to perform this action or access this resource.
Au caractère C:UsersSysAdminDesktopscripting.ps1:7 : 1
+ Add-PnPFile -Path .localFileName.docx -Folder 'Shared DocumentsBack ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Add-PnPFile], ServerUnauthorizedAccessException
    + FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Files.AddFile

Answered by J.dock on August 31, 2020

I haven't tested your PoSh, but your $SiteUrl variable should be set to https://domaincorp.sharepoint.com/sites/GED-Domain. $DocLibName would be Documents. With that said, this is much easier using the PnP cmdlets. You'll like it :-)

Connect-PnPOnline https://tenant-my.sharepoint.com/personal/username_domain_tld -SPOManagementShell
Add-PnPFile -Path .localFileName.docx -Folder 'Documents/parentFolder/subfolder'

So in this example, we connect to our personal OneDrive for Business site (you mentioned ODfB but your example shows an SPO site -- doesn't matter) then we add the file named localFileName.docx from our local computer to the Document Library named Documents (aka https://tenant-my.sharepoint.com/personal/username_domain_tld/Shared%20Documents/) and then within the Documents library, we have a parent folder and subfolder. The file will be uploaded underneath the subfolder.

Answered by Trevor Seward on August 31, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP