首页 > 解决方案 > 将 Sharepoint 文件夹(但不是实际文件夹)的内容复制到同一站点的目录中

问题描述

由于我们已经更改了有关文件保留的政策,我正在尝试从 Powershell 中的 Sharepoint Online 站点中删除一些不必要的父文件夹。我们希望摆脱预先存在的 2020 文件夹,以最大程度地减少用户导航的文件夹结构复杂性,并确保他们不会为 2021 创建文件夹并将文件复制到其中,从而占用 Sharepoint 空间。

因此,我希望将我们所有 Teams 站点中的 2020 文件夹的内容提取到“共享文档/常规”位置,然后删除空的 2020 文件夹。所以它从

'共享文档/General/2020/[所有内容]'

'共享文档/常规/[所有内容]'

MovePnP-Folder 函数自然会移动整个文件夹,但我希望删除的是文件夹本身(而不是其内容)。MovePnP-File 功能似乎以奇怪的方式工作,并在我尝试时告诉我我无权移动文件,尽管我是全局管理员。它似乎也不适用于通配符,我希望使用它来确保选择文件夹的所有内容。

我希望在我们环境中的所有 Teams 站点中重复出现这种情况。

请问我可以在这里帮忙吗?

标签: powershellfilesharepointsharepoint-onlinefile-move

解决方案


我们可以枚举文件夹并循环移动其文件/子文件夹。请参考以下演示:

#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 Move-SPOFilesBetweenFolders
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder
    )
    Try {
        #Write-host "Copying Files from '$($SourceFolder.ServerRelativeUrl)' to '$($TargetFolder.ServerRelativeUrl)'"
        #Get all Files from the source folder
        $SourceFilesColl = $SourceFolder.Files
        $Ctx.Load($SourceFilesColl)
        $Ctx.ExecuteQuery()
  
        #Iterate through each file and move
        Foreach($SourceFile in $SourceFilesColl)
        {
            #Get all files from source Folder
            $SourceFile =$Ctx.Web.GetFileByServerRelativeUrl($SourceFile.ServerRelativeUrl)
            $Ctx.Load($SourceFile)
            $Ctx.ExecuteQuery()
              
            #Move File to destination
            $TargetFileUrl = $SourceFile.ServerRelativeUrl -Replace $SourceFolderURL,$TargetFolderURL
            $SourceFile.MoveTo($TargetFileUrl, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite)
            $Ctx.ExecuteQuery()
  
            Write-host -f Green "File Moved to: "$TargetFileURL
        }
  
        #Process Sub Folders
        $SubFolders = $SourceFolder.Folders
        $Ctx.Load($SubFolders)
        $Ctx.ExecuteQuery()
        Foreach($SubFolder in $SubFolders)
        {
            If($SubFolder.Name -ne "Forms")
            {
                #Prepare Target Folder
                $EnsureFolderURL = $SubFolder.ServerRelativeUrl -Replace $SourceFolderUrl, $TargetFolderUrl
                Try {
                        $Folder=$Ctx.web.GetFolderByServerRelativeUrl($EnsureFolderURL)
                        $Ctx.load($Folder)
                        $Ctx.ExecuteQuery()
                    }
                catch {
                        #Create Folder
                        if(!$Folder.Exists)
                        {
                            $Folder=$Ctx.Web.Folders.Add($EnsureFolderURL)
                            $Ctx.Load($Folder)
                            $Ctx.ExecuteQuery()
                            Write-host "New Folder Created:"$SubFolder.Name -f Yellow
                        }
                    }
                #Call the function recursively to move all files from source folder to target
                Move-SPOFilesBetweenFolders -SiteURL $SiteURL -SourceFolder $SubFolder -TargetFolder $Folder
 
                #Remove the Source Folder
                $SubFolder.Recycle() | Out-Null
                $Ctx.ExecuteQuery()
            }
        }
    }
    Catch {
        write-host -f Red "Error Moving File:" $_.Exception.Message
    }
}
  
#Set Parameter values
$SiteURL="https://abc.sharepoint.com/sites/s01"
$SourceFolderURL ="/sites/s01/My test doc lib/SDK/testlongpath/SPServices"
$TargetFolderURL ="/sites/s01/My test doc lib"
  
#Setup Credentials to connect
$Cred= Get-Credential
  
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
       
#Get the source and Target Folders
$SourceFolder=$Ctx.Web.GetFolderByServerRelativeUrl($SourceFolderURL)
$Ctx.Load($SourceFolder)
$TargetFolder=$Ctx.Web.GetFolderByServerRelativeUrl($TargetFolderURL)
$Ctx.Load($TargetFolder)
$Ctx.ExecuteQuery()
 
#Call the function
Move-SPOFilesBetweenFolders -SiteURL $SiteURL -SourceFolder $SourceFolder -TargetFolder $TargetFolder

BR


推荐阅读