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

[玩转系统] 使用 PowerShell 将文档模板添加到 SharePoint 中的内容类型

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

使用 PowerShell 将文档模板添加到 SharePoint 中的内容类型


要求:将文档模板与 SharePoint 中的内容类型相关联。

PowerShell 将文档模板添加到 SharePoint 中的内容类型

以下是将文档模板上传到 SharePoint 中的内容类型的 PowerShell:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Parameters
$SiteURL="https://intranet.crescent.com"
$ContentTypeName="Crescent Invoice Template V2"
$DocumentTemplatePath="C:\Temp\Crescent Invoice Template V3.docx"

Try {
    #Get the Web and Content Type
    $Web = Get-SPWeb $SiteURL
    $ContentType = $Web.ContentTypes | Where {$_.Name -eq $ContentTypeName}
 
    #Check Cotent type and document template exists
    If($ContentType -ne $null -and [System.IO.File]::Exists($DocumentTemplatePath))
    {
        #Get the Template File
        $TemplateFile =(Get-ChildItem $DocumentTemplatePath).OpenRead()

        #Get the Template File Name
        $DocumentTemplateFileName = [System.IO.Path]::GetFileName($DocumentTemplatePath)

        #Upload the document Template  
        $DocumentTemplate = $ContentType.ResourceFolder.Files.Add($DocumentTemplateFileName, $TemplateFile,$True)

        #Set Document Template of the Content type
        $contentType.DocumentTemplate = $DocumentTemplateFileName
        $contentType.UpdateIncludingSealedAndReadOnly($True)
  
        Write-Host -f Green "Added Document Template $DocumentTemplateFileName to $ContentTypeName"
    }
    Else
    {
        Write-host -f Yellow "Cannot find Content Type $ContentTypeName or document template file $DocumentTemplatePath!"
    }
}
Catch {
    write-host -f Red "Error Adding Document Template to Content Type!" $_.Exception.Message
}

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

取消回复欢迎 发表评论:

关灯