网站搜索

SharePoint Online:如何使用 PowerShell 创建列表模板?


要求:在 SharePoint Online 中创建列表模板。

如何在 SharePoint Online 中创建列表模板?

SharePoint Online 中的列表模板提供可重用性,无需重新创建 SharePoint 列表中的每个字段和其他设置。例如,您可以在 SharePoint 网站集中创建一个包含所有相关列的自定义列表(例如:项目),然后您可能需要在其他网站集中使用该列表结构,而无需在每个网站上重新创建该列表。要在 SharePoint Online 中创建列表模板,请执行以下步骤:

  1. 转到 SharePoint Online 列表 >> 导航到设置 >> 列表设置。
  2. 单击“权限和管理”组下的“将列表另存为模板”。

  3. 提供文件名和模板名称。

  4. 选择适当的“包含内容”选项,然后单击“确定”在 SharePoint Online 中创建列表模板。您也可以使用上述步骤将文档库另存为 SharePoint Online 中的模板。

SharePoint Online:使用 PowerShell 将列表另存为模板

当您需要在 SharePoint Online 中重复创建包含某些特定列和项目的列表时,您可能会发现将列表另存为模板会很有帮助。然后,您可以使用模板轻松创建新的列表实例。让我们使用 PowerShell 将列表保存为 SharePoint Online 中的模板:


#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"
 
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName="Projects"

#Configure Save list as template parameters
$FileName="Projects Template"
$TemplateName="Projects Template"
$Description ="List Template for Projects"
$IncludeData = $False

Try{
    #Get 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 List
    $List = $Ctx.Web.lists.GetByTitle($ListName)
    $List.SaveAsTemplate($FileName, $TemplateName, $Description, $IncludeData)
    $Ctx.ExecuteQuery()

    Write-Host -f Green "List Saved as Template!"
}
Catch {
        write-host -f Red "Error Saving List as template!" $_.Exception.Message
} 

此 PowerShell 脚本在 SharePoint Online 中创建列表模板。

SharePoint Online 将列表保存为模板 URL - https://YourDomain.SharePoint.com/_layouts/15/savetmpl.aspx?List={LIST-GUID}

将列表另存为模板在 SharePoint Online 中不可见?修复方法如下:SharePoint Online:将列表另存为模板丢失?

PnP PowerShell 将列表保存为模板

以下是如何使用 PnP PowerShell 在 SharePoint Online 中将列表另存为模板:


#Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
$TemplateFileName = "DocumentsTemplate.stp"
$TemplateName = "DocumentsTemplate"
$TemplateDescription=""
$IncludeData = $False
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-credential)
$Context  = Get-PnPContext

#Get the List
$List = Get-PnpList -Identity $ListName

#Save List as template
$List.SaveAsTemplate($TemplateFileName, $TemplateName, $TemplateDescription, $IncludeData)
$Context.ExecuteQuery()

如果您需要创建与现有列表类似的新列表,或者想要创建可用于将来列表的模板,这会很有用。若要删除现有列表模板,请参阅如何使用 PowerShell 在 SharePoint Online 中删除列表模板?

相关文章