Document library's "Upload -> Folder" option doesn't actually upload an empty folder

Syed Hasan Abbas Naqvi 1 Reputation point
2022-01-24T05:07:04.463+00:00

One of our SharePoint Online clients raised this issue.

So what happens is that when a user tries to upload a folder to a document library in SharePoint online, only subfolders with contents inside are showing up, but not empty subfolders. They are using the "drag and drop" upload feature as a workaround to this issue to be able to upload empty folders as well.

However, I wonder if uploading a replica of a folder can be allowed in SharePoint by running a PowerShell. ( regardless of whether there are contents inside subfolders )

Kindly advise.

The same question was asked here: https://answers.microsoft.com/en-us/msoffice/forum/all/document-librarys-upload-folder-option-doesnt/49b52a11-c909-4cad-9f58-4b5140f084da

Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Echo Du_MSFT 17,316 Reputation points
    2022-01-24T08:30:56.643+00:00

    Hello @Syed Hasan Abbas Naqvi ,

    Welcome to Q&A Forum!

    Please run the below PowerShell to upload a Folder to SharePoint Online (Including Folder, Sub-Folders, and Files):

    Note: FolderBBB is an empty subfolder.

    #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"  
       
    #Function to Check if Folder Exists. If not, Create the Folder  
    Function Ensure-SPOFolder()  
    {  
        param  
        (  
            [Parameter(Mandatory=$true)] [string] $FolderRelativeURL  
        )  
       
        #Check Folder Exists  
        Try {  
            $Folder = $Web.GetFolderByServerRelativeUrl($FolderRelativeURL)  
            $Ctx.Load($Folder)  
            $Ctx.ExecuteQuery()  
        
            #Write-host -f Green "Folder Already Exists!"  
        }  
        Catch {  
            #Create New Sub-Folder  
            $Folder=$Web.Folders.Add($FolderRelativeURL)  
            $Ctx.ExecuteQuery()  
            Write-host -f Green "Created Folder at "$FolderRelativeURL  
        }  
    }  
       
    #Function to Upload a File to a SharePoint Online  
    Function Upload-SPOFile()     
    {  
        param  
        (  
            [Parameter(Mandatory=$true)] [string] $SourceFilePath,  
            [Parameter(Mandatory=$true)] [string] $TargetFileURL  
        )  
           
        #Get the file from disk  
        $FileStream = ([System.IO.FileInfo] (Get-Item $SourceFilePath)).OpenRead()  
        #Get File Name from source file path  
        $SourceFileName = Split-path $SourceFilePath -leaf  
          
        #Upload the File to SharePoint Library  
        $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  
    }  
        
    #Main Function to upload a Local Folder to SharePoint Online Document Library Folder  
    Function Upload-SPOFolder()  
    {  
        param  
        (  
            [Parameter(Mandatory=$true)] [string] $SourceFolderPath,  
            [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder         
        )  
       
        #Get All Files and Sub-Folders from Source  
        Get-ChildItem $SourceFolderPath -Recurse | ForEach-Object {  
            If ($_.PSIsContainer -eq $True)  
            {  
                $FolderRelativeURL = $TargetFolder.ServerRelativeURL+$_.FullName.Replace($SourceFolderPath,[string]::Empty).Replace("\","/")  
                If($FolderRelativeURL)  
                {  
                    Write-host -f Yellow "Ensuring Folder '$FolderRelativeURL' Exists..."  
                    Ensure-SPOFolder -FolderRelativeURL $FolderRelativeURL  
                }   
            }  
            Else  
            {  
                $FolderRelativeUrl = $TargetFolder.ServerRelativeURL + $_.DirectoryName.Replace($SourceFolderPath,[string]::Empty).Replace("\","/")  
                $FileRelativeURL = $FolderRelativeUrl+"/"+$_.Name  
                Write-host -f Yellow "Uploading File '$_' to URL "$FileRelativeURL  
                Upload-SPOFile -SourceFilePath $_.FullName -TargetFileURL $FileRelativeURL  
            }  
        }  
    }  
       
    #Set parameter values  
    $SiteURL="https://domain.sharepoint.com/sites/sitename"  
    $LibraryName="Documents"  
    $SourceFolderPath="C:\temp\file"  
        
    #Setup Credentials to connect  
    $Cred= Get-Credential  
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)  
        
    #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)  
    $List = $Web.Lists.GetByTitle($LibraryName)  
    $Ctx.Load($List)  
    $Ctx.Load($List.RootFolder)  
    $Ctx.ExecuteQuery()  
        
    #Call the function to Upload All files & folders from local folder to SharePoint Online  
    Upload-SPOFolder -SourceFolderPath $SourceFolderPath -TargetFolder $List.RootFolder  
    

    167659-folder1.jpg

    167660-folder2.jpg

    Here is my test:

    167755-folder3.jpg

    167792-folder4.jpg

    Thanks,
    Echo Du

    =============================

    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.

    1 person found this answer helpful.

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.