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

[玩转系统] SharePoint Online:使用 PowerShell 创建内容类型

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

SharePoint Online:使用 PowerShell 创建内容类型


SharePoint Online 中的内容类型是什么?

内容类型是一组组合在一起以服务于特定目的的列。考虑一下发票模板!您和您的用户不必每次都从头开始创建不同的模板,您可以定义一次发票模板,然后您的用户就可以开始在任意数量的列表或文档库中使用它!通过这种方式,您可以在协作环境中强制标准化。或者考虑一个“项目跟踪”列表,其中包含跟踪组织项目所需的列。您无需每次都为项目跟踪列表创建和添加每一列,只需单击几下即可在任意数量的列表中使用项目跟踪内容类型模板。

SharePoint Online 列表或文档库可以包含多种内容类型。例如,您可以将多种内容类型(例如“发票”、“提案”、“采购订单”等)添加到库中,以将与项目相关的所有内容组织为单个实体。将内容类型与 SharePoint Online 列表或库关联后,SharePoint 允许您通过该列表或库中的“新建项目”命令创建该内容类型的新项目。对于文档内容类型,您还可以定义一个文档模板,该模板将成为从此特定内容类型创建的所有文档的基础。

作为最佳实践,内容类型最好在顶部站点定义,独立于任何列表或库。它可以在下面的任何 SharePoint 网站上重复使用,甚至可以在具有内容类型中心的 SharePoint 网站集中重复使用。

如何在 SharePoint Online 中创建内容类型?

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

  1. 转到站点设置>>单击“Web Designer Gallery”组下的“Site Content Types”。
  2. 在网站内容类型页面上,您将看到按部分分组的默认内容类型列表,例如项目、任务、文档等。单击顶部的“创建”链接。
  3. 为您的自定义内容类型提供名称。或者,您可以输入新内容类型的说明以使其清晰可见。
  4. 选择您的内容类型所基于的父内容类型,例如“项目”。您可以创建新的内容类型组或选择现有的内容类型组。

    [玩转系统] SharePoint Online:使用 PowerShell 创建内容类型

  5. 单击“确定”完成添加内容类型。创建内容类型后,下一步是将所需的网站列添加到内容类型,以使元数据可从内容类型获取。

现在,让我们看看在 SharePoint 中以编程方式创建内容类型的 PowerShell 脚本。

SharePoint Online:使用 PowerShell 创建内容类型

可以借助 PowerShell 自动创建自定义内容类型。以下是如何使用 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 turn ON Content Type in SharePoint Online list or library
Function Create-SPOContentType()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $CTypeName,
        [Parameter(Mandatory=$true)] [string] $CTypeDesc,
        [Parameter(Mandatory=$true)] [string] $ParentCTypeName,
        [Parameter(Mandatory=$true)] [string] $CTypeGroup
    )

    Try {
        $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 content types from the site
        $ContentTypeColl = $Ctx.web.ContentTypes
        $Ctx.Load($ContentTypeColl)
        $Ctx.ExecuteQuery()

        #Get the parent content type
        $ParentCType = $ContentTypeColl| Where {$_.Name -eq $ParentCTypeName}

        #Check if content type exists already
        $ContentType = $ContentTypeColl| Where {$_.Name -eq $CTypeName}


        If($ContentType -ne $Null)
        {
            Write-host "Content type '$CTypeName' already exists!" -ForegroundColor Yellow
        }
        else
        {
            #Specify properties for the new content type
            $CTypeCreationInfo=New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation
            $CTypeCreationInfo.Name=$CTypeName
            $CTypeCreationInfo.Description=$CTypeDesc
            $CTypeCreationInfo.Group=$CTypeGroup
            $CTypeCreationInfo.ParentContentType=$ParentCType

            # sharepoint online powershell create content type
            $ContentType=$ContentTypeColl.Add($CTypeCreationInfo)
            $Ctx.ExecuteQuery()
           
            Write-host "Content Type '$CTypeName' Created Successfully!" -ForegroundColor Green
        }
    }
    Catch {
        write-host -f Red "Error Creating Content Type!" $_.Exception.Message
    } 
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$CTypeName="Projects"
$CTypeDesc="Content type for Project template"
$ParentCTypeName="Item"
$CTypeGroup="Crescent Projects"

#Call the function
Create-SPOContentType -SiteURL $SiteURL -CTypeName $CTypeName -CTypeDesc $CTypeDesc -ParentCTypeName $ParentCTypeName -CTypeGroup $CTypeGroup

此脚本根据指定的父内容类型在 SharePoint Online 中创建内容类型。在此示例中,我从“Item”内容类型创建了“Project”内容类型,该内容类型的默认字段为“Title”。您可能想要向内容类型添加其他字段。以下是如何使用 PowerShell 将列添加到 SharePoint Online 中的内容类型:如何使用 PowerShell 将网站列添加到 SharePoint Online 中的内容类型?

使用 PnP PowerShell 在 SharePoint Online 中创建内容类型

以下是使用 Add-PnPContentType cmdlet 在 SharePoint Online 中创建内容类型的 PnP PowerShell 脚本:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName ="Crescent Projects V3"
$ContentTypeDescription ="Base Content Type for Crescent Projects Template"
$ContentTypeGroupName = "Crescent Content Types"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Create Content Type
Add-PnPContentType -Name $ContentTypeName -Description $ContentTypeDescription -Group $ContentTypeGroupName

这将基于默认父项“Item”创建一个新的内容类型。

[玩转系统] SharePoint Online:使用 PowerShell 创建内容类型

您可以将父内容类型指定为:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName ="Crescent Project Proposal V1"
$ContentTypeDescription ="Base Content Type for Crescent Project Prposal Template"
$ContentTypeGroupName = "Crescent Content Types"
$ParentContentTypeName ="Document" 

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Parent content type
$ParentContentType = Get-PnPContentType -Identity $ParentContentTypeName

#Create Content Type
Add-PnPContentType -Name $ContentTypeName -Description $ContentTypeDescription -Group $ContentTypeGroupName -ParentContentType $ParentContentType

内容类型准备就绪后,您可以将内容类型添加到列表和库:如何将内容类型添加到 SharePoint Online 中的列表?

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

取消回复欢迎 发表评论:

关灯