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

[玩转系统] 如何使用 PowerShell 将列表模板上传到 SharePoint Online?

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

如何使用 PowerShell 将列表模板上传到 SharePoint Online?


要求:使用 PowerShell 在 SharePoint Online 中导入列表模板。

为什么我们需要 SharePoint Online 中的列表模板?那么,如果您在 SharePoint Online 中有一个包含一组列和自定义的列表,则可以在任何其他网站集中重复使用它,而无需从头开始重新创建该列表。列表模板包含字段、公式、格式和可选数据。列表模板可以下载并上传到其他站点。在本教程中,我们将指导您完成在 SharePoint Online 中导入列表模板的过程,以便您可以开始在 SharePoint 网站上使用它。

SharePoint Online 列表模板库 URL
在 SharePoint Online URL 中列出模板库:https://yourdomain.sharepoint.com/_catalogs/lt/Forms/AllItems.aspx

如何在 SharePoint Online 中导入列表模板?

SharePoint 列表模板可用于快速创建与现有列表或库具有相同结构和内容的新列表或库。导入列表模板允许您在新列表或库中重复使用预先设计的设置、视图和数据集合。此功能可以节省创建新列表或库的时间和精力,因为您不必从头开始手动创建每个列表或库。

下载的列表模板 (.STP) 应上传到任何网站集的列表模板库以便重复使用。以下是将列表模板上传到 SharePoint Online 的方法:

  1. 转到“设置”>>“站点设置”页面>>单击“Web 设计器库”下的“列表模板”(SharePoint Online 中缺少列表模板库?修复方法如下:SharePoint Online 中不提供列表模板库?)
  2. 单击功能区“文件”选项卡中的“上传”按钮

    [玩转系统] 如何使用 PowerShell 将列表模板上传到 SharePoint Online?

  3. 上传您从网站集中下载的 .stp 文件。

    [玩转系统] 如何使用 PowerShell 将列表模板上传到 SharePoint Online?

  4. 为模板提供名称和标题,然后单击“保存”。

    [玩转系统] 如何使用 PowerShell 将列表模板上传到 SharePoint Online?

现在,您的列表模板应该可以作为您上传列表模板的网站集中的应用程序使用。让我们看看如何使用 SharePoint Online PowerShell 上传列表模板。

使用 PowerShell 在 SharePoint Online 中上传列表模板

以下是如何使用 PowerShell 在 SharePoint Online 中导入列表模板:


#Load SharePoint CSOM 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"

#Function to Upload a list template To SharePoint Online using powershell
Function Upload-SPOListTemplate
{
    param
    (
        [string]$SiteURL  = $(throw "Enter the Site URL!"),
        [string]$ListTemplateName = $(throw "Enter the List Template Name!"),
        [string]$ListTemplateFile = $(throw "Enter the File Name to Upload List Template!")
    )
    Try {
        #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 the "List Templates" Library
        $List= $Ctx.web.Lists.GetByTitle("List Template Gallery")
        $ListTemplates = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
        $Ctx.Load($ListTemplates)
        $Ctx.ExecuteQuery()

        #Check if the Given List Template already exists
        $ListTemplate = $ListTemplates | where { $_["TemplateTitle"] -eq $ListTemplateName }

        If($ListTemplate -eq $Null)
        {
            #Get the file from disk
            $FileStream = ([System.IO.FileInfo] (Get-Item $ListTemplateFile)).OpenRead()
            #Get File Name from source file path
            $TemplateFileName = Split-path $ListTemplateFile -leaf
   
            #Upload the File to SharePoint Library
            $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
            $FileCreationInfo.Overwrite = $true
            $FileCreationInfo.ContentStream = $FileStream
            $FileCreationInfo.URL = $TemplateFileName
            $FileUploaded = $List.RootFolder.Files.Add($FileCreationInfo)
            $Ctx.Load($FileUploaded) 
            $Ctx.ExecuteQuery() 
            
            #Set Metadata of the File
            $ListItem = $FileUploaded.ListItemAllFields
            $Listitem["TemplateTitle"] = $ListTemplateName
            $Listitem["FileLeafRef"] = $ListTemplateName
            $ListItem.Update()
            $Ctx.ExecuteQuery()
 
            #Close file stream
            $FileStream.Close()

            write-host -f Green "List Template '$ListTemplateFile' Uploaded to $SiteURL"
        }
        else
        {
            Write-host -f Yellow "List Template '$ListTemplateName' Already Exists"
        }
    }
    Catch {
        write-host -f Red "Error Uploading List Template!" $_.Exception.Message
    } 
}

#Variables
$SiteURL = "https://Crescent.sharepoint.com/"
$ListTemplateName= "Projects Template V4"
$ListTemplateFile = "C:\Temp\CrescentProject.stp"

#Call the function to Download the list template
Upload-SPOListTemplate -SiteURL $SiteURL -ListTemplateName $ListTemplateName -ListTemplateFile $ListTemplateFile

这将使用 PowerShell 将列表模板上传到 SharePoint Online。要将列表模板上传到 SharePoint Online,请使用:SharePoint Online PowerShell 上传列表模板

如何将 CSV 文件中的列表模板上传到多个网站?

假设您有一个包含网站 URL 列表的 CSV 文件,并且希望将列表模板上传到 CSV 文件中的所有网站。以下是如何使用 PowerShell 将列表定义 STP 部署到 SharePoint Online。

[玩转系统] 如何使用 PowerShell 将列表模板上传到 SharePoint Online?

只需替换上述脚本中第 75 行的此脚本即可。


#Read CSV file
$CSVData = Import-CSV "C:\Temp\sites.csv" -Header SiteURL

#Iterate through each row
ForEach($Row in $CSVData)
{
    Write-host "Uploading to Site:"$Row.SiteURL
    
    #Call the Function to upload list template to site        
    Upload-SPOListTemplate -SiteURL $Row.SiteURL -ListTemplateName $ListTemplateName -ListTemplateFile $ListTemplateFile
}

最后但并非最不重要的一点是:您必须启用自定义脚本才能在您的网站上启用“列表模板”:如何在 SharePoint Online 中启用自定义脚本?

结论:

通过执行本教程中概述的步骤,您将能够在 SharePoint Online 中导入列表模板,并开始使用它来创建与原始列表或内容具有相同结构和内容的新列表或库。此功能可以节省创建新列表或库的时间和精力,并帮助您在整个 SharePoint 网站中保持一致的结构和内容。

上传列表模板后,您可以通过 Web 浏览器界面或使用 PowerShell 从模板创建列表的新实例:如何在 SharePoint Online 中从模板创建列表?

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

取消回复欢迎 发表评论:

关灯