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

[玩转系统] SharePoint Online:使用 PowerShell 通过文档模板创建内容类型

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

SharePoint Online:使用 PowerShell 通过文档模板创建内容类型


要求:使用文档模板添加新的 SharePoint Online 内容类型。

如何在 SharePoint Online 中使用模板创建内容类型?

内容类型是一个可重用的组件,具有相关列(或元数据)的集合。例如,“发票模板”将具有相关列,例如:发票编号、日期、客户名称、总金额等。内容类型也可能具有模板。

在 SharePoint Online 中,您可以使用文档模板创建内容类型。这使您可以轻松创建每次都采用相同格式的新文档。模板可以采用 Office 支持的任何格式,例如 Word、Excel 或 PowerPoint。这篇博文将展示如何使用文档模板创建内容类型。我们还将展示如何使用 PowerShell 通过文档模板创建内容类型。这对于自动化在 SharePoint Online 中创建新内容类型和文档的过程非常有用。

要在 SharePoint Online 中添加带有模板的内容类型,请执行以下步骤:

  1. 单击设置齿轮>>转到站点信息>>查看所有站点设置。
  2. 单击“Web Designer Gallery”组下的“Site Content Types”。
  3. 在“网站内容类型”页面中,您将看到按部分分组的默认内容类型列表,例如项目、任务、文档等。单击顶部的“创建”链接。

    [玩转系统] SharePoint Online:使用 PowerShell 通过文档模板创建内容类型

  4. 为您的自定义内容类型提供名称。或者,您可以输入新内容类型的说明以使其清晰可见。
  5. 选择您的内容类型所基于的父内容类型作为“文档”。您可以创建新的内容类型组或选择任何现有的内容类型组。
  6. 单击“确定”完成添加内容类型。

    [玩转系统] SharePoint Online:使用 PowerShell 通过文档模板创建内容类型

创建内容类型后,下一步是添加文档模板。

  1. 在内容类型设置页面中,单击“高级”链接。
  2. 选择“上传新文档模板”,浏览并选择您的文档模板。

    [玩转系统] SharePoint Online:使用 PowerShell 通过文档模板创建内容类型

  3. 单击底部的“确定”按钮保存所有更改。从内容类型设置页面发布一次内容类型。

您可以使用此方法根据预定义模板快速创建新文档。

在“内容类型中心”中创建内容类型是个好主意!您可以通过浏览以下 URL 启动内容类型中心:https://YourTenantUrl/sites/ContentTypeHub

PowerShell 使用文档模板创建内容类型

现在,让我们使用 SharePoint Online 中的 PowerShell 脚本创建带有文档模板的内容类型。


#Parameters
$ContentTypeHubURL = "https://crescent.sharepoint.com/sites/contentTypeHub"
$ContentTypeName = "Crescent Invoice Template V2"
$TemplateFilePath = "C:\Temp\Document Template\Invoice-template.docx"
#content type template location - Server Relative URL
$TemplateFolder = "/Sites/ContentTypeHub/_cts/$ContentTypeName" 

Try { 
    #Connect to PnP Online
    Connect-PnPOnline -Url $ContentTypeHubURL -Interactive

    #Upload the document template to the corresponding folder of the content type - Site Relative URL
    $TemplateFile = Add-PnPFile -Path $TemplateFilePath -Folder $TemplateFolder
 
    #Create Content Type with Document Template
    Add-PnPContentType -Name $ContentTypeName -ParentContentType (Get-PnPContentType -Identity 0x0101) -DocumentTemplate $TemplateFile.ServerRelativeUrl
}
Catch {
    Write-host -f Red "Error:" $_.Exception.Message
}

使用新模板更新令人兴奋的内容类型怎么样?


#Parameters
$ContentTypeHubURL = "https://crescent.sharepoint.com/sites/contentTypeHub"
$ContentTypeName = "Crescent Invoice Template"
$TemplateFilePath = "C:\Temp\Document Template\Invoice-template-v2.docx"
#content type template location - Server Relative URL
$TemplateFolder = "/Sites/ContentTypeHub/_cts/$ContentTypeName" 

Try { 
    #Connect to PnP Online
    Connect-PnPOnline -Url $ContentTypeHubURL -Interactive

    #Upload the document template to the corresponding folder of the content type - Site Relative URL
    $TemplateFile = Add-PnPFile -Path $TemplateFilePath -Folder $TemplateFolder
 
    #Get the Content Type
    $ContentType = Get-PnPContentType -Identity $ContentTypeName

    #Update the document template for the content type
    $ContentType.DocumentTemplate = $TemplateFile.Name
    $ContentType.Update($true)
    Invoke-PnPQuery
}
Catch {
    Write-host -f Red "Error:" $_.Exception.Message
}

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

取消回复欢迎 发表评论:

关灯