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

[玩转系统] SharePoint Online:使用 PowerShell 从自定义模板创建列表

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

SharePoint Online:使用 PowerShell 从自定义模板创建列表


要求:使用 SharePoint Online 中的 PowerShell 从自定义列表模板创建列表。

SharePoint Online:如何从模板创建列表?

SharePoint Online 中的列表模板通过重用任何现有列表或文档库(其中包含所有列以及列表中的数据(可选))来节省大量时间。您可以从列表模板创建任意数量的副本。

假设您的网站上已有列表模板,以下是如何从模板 SharePoint Online 创建列表:

  1. 登录到您的 SharePoint Online 网站 >> 单击“设置”齿轮图标,然后单击“添加应用程序”。
  2. 您可以使用分页来查找列表模板,或使用“查找应用程序”搜索框来选择列表模板。

    [玩转系统] SharePoint Online:使用 PowerShell 从自定义模板创建列表

  3. 为新列表提供名称,然后单击“确定”。这将根据 SharePoint Online 中的模板创建一个新列表。
  4. 如果您在最初创建列表模板时选择了“包含内容”,您将看到使用相同列表架构创建的新列表以及数据。让我们看看如何使用 SharePoint Online PowerShell 从模板创建列表。

SharePoint Online:使用 PowerShell 从模板创建列表

列表是 SharePoint 用于存储信息的主要构建块。让我们在 SharePoint Online 中使用 PowerShell 从模板创建列表。


#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Config Variables
$SiteURL="https://crescent.sharepoint.com"
$ListTemplateName = "Quick Links Template"
$ListName="Quick Links V2"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get All Lists
$Lists = $Ctx.Web.Lists
$Ctx.Load($Lists)
$Ctx.ExecuteQuery()

#Get the Custom list template
$ListTemplates=$Ctx.site.GetCustomListTemplates($Ctx.site.RootWeb)
$Ctx.Load($ListTemplates)
$Ctx.ExecuteQuery()

#Filter Specific List Template
$ListTemplate = $ListTemplates | where { $_.Name -eq $ListTemplateName } 
If($ListTemplate -ne $Null)
{
    #Check if the given List exists
    $List = $Lists | where {$_.Title -eq $ListName}
    If($List -eq $Null)
    {
        #Create new list from custom list template
        $ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation
        $ListCreation.Title = $ListName
        $ListCreation.ListTemplate = $ListTemplate
        $List = $Lists.Add($ListCreation)
        $Ctx.ExecuteQuery()
        Write-host -f Green "List Created from Custom List Template Successfully!"
    }
    else
    {
        Write-host -f Yellow "List '$($ListName)' Already Exists!"
    }
}
else
{
    Write-host -f Yellow "List Template '$($ListTemplateName)' Not Found!"
}

相同的方法适用于从自定义模板创建文档库。

SharePoint Online:PnP PowerShell 从模板创建列表

列表是在 SharePoint 中存储信息的主要构建块。以下是从模板创建列表的 PnP PowerShell:


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Migation Tasks"
$ListTemplateName ="Project Tasks"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Custom List Template
$Ctx = Get-PnPContext
$Web = Get-PnPWeb
$RootWeb = $Ctx.Site.RootWeb
$ListTemplates = $Ctx.Site.GetCustomListTemplates($RootWeb)
$Ctx.Load($RootWeb)
$Ctx.Load($ListTemplates)
Invoke-PnPQuery
$ListTemplate = $ListTemplates | where {$_.Name -eq $ListTemplateName}
 
#sharepoint online powershell create list from template
$ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListCreation.Title = $ListName
$ListCreation.ListTemplate = $ListTemplate
$Web.Lists.Add($ListCreation)
Invoke-PnPQuery

按照本文概述的步骤,您可以从预构建的模板创建新列表,自定义列表以满足您的特定需求,然后开始向列表添加数据。

  • SharePoint Online:使用 PowerShell 获取列表模板
  • SharePoint Online:使用 PowerShell 复制文档库
  • 使用 PowerShell 在 SharePoint Online 中获取网站模板
  • SharePoint Online:如何使用 PowerShell 删除列表模板?

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

取消回复欢迎 发表评论:

关灯