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

[玩转系统] SharePoint Online:使用 PowerShell 将列从一个列表复制到另一个列表

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

SharePoint Online:使用 PowerShell 将列从一个列表复制到另一个列表


要求:将列复制到 SharePoint Online 中的另一个列表。

如何在 SharePoint Online 的列表之间复制列?

您是否曾想将一个列从一个 SharePoint 列表复制到另一个?也许您想在多个列表中使用同一列,您想将数据从一个列表复制到另一个列表,或者您希望将数据从一个列表移动到另一个列表。不管是什么原因,您可以使用以下方法:

通过一一重新创建来复制 Web UI 中的列:

  1. 登录到您的 SharePoint Online 网站 >> 转到要从中复制列的列表。
  2. 单击右上角的设置齿轮图标,然后选择“列表设置”。
  3. 在列表设置页面下的“列”中,找到要复制的列,然后单击列名称以打开其属性。
  4. 在新选项卡中浏览至浏览器中的目的地列表。进入其设置,然后单击设置页面上的“创建列”。
  5. 开始将列设置从源复制到目标:例如,列名称、描述和其他设置。单击“确定”最终创建列。

    [玩转系统] SharePoint Online:使用 PowerShell 将列从一个列表复制到另一个列表

  6. 对要复制的每个附加列重复步骤 3-5。

PowerShell 将列从一个列表复制到另一个列表

如果您有许多列需要复制,则使用 Web UI 重新创建列可能会很麻烦;幸运的是,使用 PowerShell 脚本很容易做到这一点!


#Parameters
$SourceSiteUrl = "https://crescent.sharepoint.com/sites/Retail"  
$DestinationSiteUrl = "https://crescent.sharepoint.com/sites/Sales"
$SourceListName = "Projects"
$DestinationListName = "Projects"
$FieldsToCopy = "ProjectName", "ProjectDescription", "ProjectManager", "Department" #Internal Names

#Connect to the Source site
Connect-PnPOnline -Url $SourceSiteUrl -Interactive
 
#Get All Fields from the Source List
$SourceListFields = Get-PnPField -List $SourceListName

#Connect to Destination site
Connect-PnPOnline -Url $DestinationSiteUrl -Interactive

#Get All Fields from the Desntination List
$DestinationListFields = Get-PnPField -List $DestinationListName

#Copy columns from the Source List to Destination List
ForEach($Field in $FieldsToCopy)
{
    #Check if the destination list has the field already
    $DestinationFieldExist = ($DestinationListFields | Select -ExpandProperty InternalName).Contains($Field)
    If($DestinationFieldExist -eq $false)
    {
        #Get the field to copy
        $SourceField = $SourceListFields | Where {$_.InternalName -eq $Field}
        If($SourceField -ne $Null)
        {
            Add-PnPFieldFromXml -List $DestinationListName -FieldXml $SourceField.SchemaXml | Out-Null
            Write-Host "Copied Field from Source to Destination List:"$Field -f Green
        }
        Else
        {
            Write-Host "Field '$Field' does not Exist in the Source List!" -f Yellow
        }
    }
    Else
    {
        Write-host "Field '$Field' Already Exists in the Destination List!" -f Yellow
    }
}

复制除只读、隐藏和一些特殊字段之外的所有字段怎么样?当然!使用:


$FieldsToCopy = Get-PnPField -List $SourceListName | Where {-not ($_.ReadOnlyField) -and (-Not ($_.Hidden)) -and ($_.InternalName -ne  "ContentType") -and ($_.InternalName -ne  "Attachments") } | Select -ExpandProperty InternalName

请注意,上述方法仅将列从一个列表复制到另一个列表。如果您还想复制 SharePoint 列表列值,请使用:如何将数据从 SharePoint Online 列表中的一列复制到另一列?

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

取消回复欢迎 发表评论:

关灯