Quantcast
Channel: Microsoft Online: SharePoint Online forum
Viewing all articles
Browse latest Browse all 10096

Uploading files to SP Online in 3 Level sub folder

$
0
0

Hi, 

I am trying to upload files to SP Online using PS script. I can upload files in Folder of Document Library. So here are the parameters i am using

$User = "abc@abc.com"
$SiteURL = "https://abc.sharepoint.com/teams/messaging"
$Folder = "C:\Users\name\Test"
$DocLibName = "Document Library"
$FolderName = "Exchange"

This is uploading files into Exchange folder of Document library. I want to upload files under sub folder of Exchange. So the path is Document Library/Exchange/folder1/folder2. I want to upload files in Folder2 but script is only copying in Exchange. Is there a ways to copy files to Folder2 in SP Online. Below is the complete script:

$User = "abc@abc.com"
$SiteURL = "https://abc.sharepoint.com/teams/messaging"
$Folder = "C:\Users\name\Test"
$DocLibName = "Document Library"
$FolderName = "Exchange"

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

#Retrieve folder
$FolderToBindTo = $List.RootFolder.Folders
$Context.Load($FolderToBindTo)
$Context.ExecuteQuery()
$FolderToUpload = $FolderToBindTo | Where {$_.Name -eq $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
$Upload = $FolderToUpload.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}



Hasan


Viewing all articles
Browse latest Browse all 10096

Trending Articles



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