当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 SharePoint Designer - PowerShell 将附件从 SharePoint 列表复制到文档库

作者:精品下载站 日期:2024-12-14 13:43:06 浏览:15 分类:玩电脑

使用 SharePoint Designer - PowerShell 将附件从 SharePoint 列表复制到文档库


要求:将附件从 SharePoint 列表复制到文档库

如何将附件从列表复制到 SharePoint 中的文档库?

SharePoint 将附件保存在“列表>>附件>>‘列表项目 ID’”文件夹路径下。因此,如果您想将附件从列表复制到文档库,请按照下列步骤操作:

  1. 从 SharePoint Designer 打开您的 SharePoint 网站
  2. 导航到“所有文件”视图>>列表>>您的源列表>>附件文件夹。此处,文件夹是根据列表项的 ID 创建的。

    [玩转系统] 使用 SharePoint Designer - PowerShell 将附件从 SharePoint 列表复制到文档库

  3. 只需从这些文件夹复制附件文件并导航到目标文档库并粘贴到那里即可。

    [玩转系统] 使用 SharePoint Designer - PowerShell 将附件从 SharePoint 列表复制到文档库

好吧,如果您有大量列表项/附件,将列表附件复制到文档库会很乏味。因此,让我们在 SharePoint 中使用 PowerShell 将列表附件复制到文档库。

用于将附件从列表复制到文档库的 PowerShell 脚本:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$WebURL = "https://sharepoint.crescent.com/pmo/GIS/"

$SourceListName = "External Projects"   
$TargetLibraryName = "Design Documents"

#Get the Web List and Library objects
$web = Get-SPWeb $WebURL
$SourceList = $web.Lists[$SourceListName]    
$TargetLibrary = $web.Lists[$TargetLibraryName]    

#Loop through each list item
foreach ($ListItem in $SourceList.Items)
{
   if($ListItem.Attachments.Count -gt 0)
   {
        #Loop through each attachment in the list item
        foreach ($Attachment in $ListItem.Attachments)    
        {   
              #Get the attachment
              $file = $web.GetFile($ListItem.Attachments.UrlPrefix+$Attachment)        
              $bytes = $file.OpenBinary()               
    
              $TargetFileName = $TargetLibrary.RootFolder.Url+"/"+$Attachment
              $TargetFile = $TargetLibrary.RootFolder.Files.Add($TargetFileName, $bytes, $true)
              Write-Host "Copied to: $($TargetFilename)"
        }
   }
}

此脚本将所有列表项中的所有附件复制到给定库的根文件夹。您也可以稍微调整脚本以在目标库中创建子文件夹。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯