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

[玩转系统] 如何使用 PowerShell 复制 SharePoint 列表?

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

如何使用 PowerShell 复制 SharePoint 列表?


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

如何在 SharePoint 中复制列表?

您需要制作 SharePoint 列表的副本吗?也许您想要备份列表中的数据或创建一个具有与现有列表相同的列集和设置的新列表!无论如何,使用 PowerShell 是最快、最简单的方法。本文将向您展示如何使用 PowerShell 复制 SharePoint 列表。

虽然导出-导入是一个选项,但您可以将包含或不包含内容的列表保存为模板,并从列表模板创建任意数量的新列表或库。进入列表设置,将列表另存为模板,然后创建新列表并选择列表模板!

[玩转系统] 如何使用 PowerShell 复制 SharePoint 列表?

PowerShell 在 SharePoint 中克隆列表

以下是使用“将列表另存为模板”和“从模板创建列表”方法复制 SharePoint 列表结构的 PowerShell:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Parameters
$SiteURL="https://intranet.crescent.com"
$ListName="Projects"
$NewListName = "Projects Archive"
$SaveData= $False

Try {
    #Get the web and List
    $Web = Get-SPWeb $SiteURL
    $List = $Web.Lists[$ListName]

    #Check if the new list doesn't exists
    If($Web.Lists.TryGetList($NewListName) -eq $null)
    {
        #Save list as template
        $List.SaveAsTemplate($List.ID.ToString(), $List.ID.ToString(), [string]::Empty, $SaveData)

        #Get the List template
        $ListTemplate = $web.Site.GetCustomListTemplates($web)[$List.ID.ToString()]

        #Clone list
        $NewList = $web.Lists.Add($NewListName, "$($NewListName)-$($List.Description)", $ListTemplate)

        #Remove the List template file Created
        $ListTemplateFile = $web.Site.RootWeb.GetFolder("_catalogs/lt").Files | where {$_.Name -eq $ListTemplate.InternalName}
        $ListTemplateFile.Delete()

        write-host -f Green "List '$ListName' Cloned to '$NewListName!'"
    }
    Else
    {
        write-host -f Yellow "List '$NewListName' already exists!"
    }

}
Catch {
    write-host -f Red "Error Adding Template to Document Library!" $_.Exception.Message
} 

您可以使用此脚本复制包含内容或仅列表结构的 SharePoint 列表。您可以将 $SaveData 标志设置为 $True 以包含列表的内容。在 SharePoint Online 中复制列表: 如何在 SharePoint Online 中复制列表?

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

取消回复欢迎 发表评论:

关灯