How to add the list item in the folder on SharePoint online by PowerShell

WonChul Choi 0 Reputation points
2023-11-16T03:04:18.37+00:00

Hello I try to add list items inside folder on SharePoint Online using by PowerShell script.

I use SharePoint.Client package because I updating the script

Anyway, I success to get list items and get sub folders in the List. However, I cannot add list items under sub folders. I keep failing to add inside list folder.

Could you please help me to add list item inside Sub folders ?

Script : RootFolder.Folders

$boxesList = $Context.web.Getlist($boxesListUrl)
    $allSubFolders = $boxesList.RootFolder.Folders
    $Context.Load($boxesList)
    $Context.Load($allSubFolders)

    $Context.ExecuteQuery()
    
    $subFolder = $allSubFolders | where { $_.Name -eq $deptCode }
    
    $Context.Load($subFolder)
    $Context.ExecuteQuery()
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,708 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,523 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 35,556 Reputation points Microsoft Vendor
    2023-11-16T06:30:26.7133333+00:00

    Hi @WonChul Choi,

    If you want to add a file to subfolder by powershell, you could refer to following script

    #Load SharePoint CSOM Assemblies
    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"
     
    #Set parameter values
    $SiteURL="https://xxx.sharepoint.com/sites/testsite"
    $SourceFilePath="C:\Users\testa.doc"
    $TargetFolderRelativeURL ="/sites/testsite/Shared Documents/sufolder/folder"
     
    #Setup Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
      
    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
           
        #Get the Target Folder to upload
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $TargetFolder = $Web.GetFolderByServerRelativeUrl($TargetFolderRelativeURL)
        $Ctx.Load($TargetFolder)
        $Ctx.ExecuteQuery()
     
        #Get the source file from disk
        $FileStream = ([System.IO.FileInfo] (Get-Item $SourceFilePath)).OpenRead()
        #Get File Name from source file path
        $SourceFileName = Split-path $SourceFilePath -leaf  
        $TargetFileURL = $TargetFolderRelativeURL+"/"+$SourceFileName
     
        #Upload the File to SharePoint Library Folder
        $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
        $FileCreationInfo.Overwrite = $true
        $FileCreationInfo.ContentStream = $FileStream
        $FileCreationInfo.URL = $TargetFileURL
        $FileUploaded = $TargetFolder.Files.Add($FileCreationInfo) 
        $Ctx.ExecuteQuery() 
     
        #Close file stream
        $FileStream.Close()
        Write-host "File '$TargetFileURL' Uploaded Successfully!" -ForegroundColor Green
    }
    catch {
        write-host "Error Uploading File to Folder: $($_.Exception.Message)" -foregroundcolor Red
    }
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. WonChul Choi 0 Reputation points
    2023-11-16T13:19:43.04+00:00

    Hello,

    In fact, I know how to upload files under folder. However, my team created folders in the list to make list items categorize.

    Basically, when click the folder on list, sub list will be pop up on sharepoint online.

    Is there any way to add list items under sub list or sub folder?

    Thanks,


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.