I'm new to PowerShell and Sharepoint Online and I need to upload folders and files recursively to a document library in SharePoint.
I am able to create the top-level folder but the files are uploaded outside of it. When it comes to the sub-folder it says "Access to the path is denied"
How do I upload the files into the folder and how do I upload the rest of the folders?
This is my code so far, can anyone help me?
Thanks!
#Specify tenant admin and site URL $User = "admin@company.net" $SiteURL = "https://company.sharepoint.com/sites/TestSite" $Folder = "C:\FolderStructure to upload" $DocLibName = "Admin" #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" $Password = Read-Host -Prompt "Please enter your password" -AsSecureString #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.ExecuteQuery() Function ImportFiles() { #Get name of folder $NewFolder = Split-Path $Folder -Leaf $UploadFolder = $List.RootFolder.Folders.Add($NewFolder) $Context.Load($UploadFolder) $Context.ExecuteQuery() #Loop Through Folder Foreach ($File in (dir $Folder)) { #If file If($File) { #Upload file to Sharepoint $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.Files.Add($FileCreationInfo) $Context.Load($Upload) $Context.ExecuteQuery() } #else #foreach (folder) #recurse method Else { recurse ImportFiles } } } ImportFiles
Dearbhla Bradley