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

[玩转系统] 如何使用 PowerShell 在 SharePoint 中创建内容类型?

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

如何使用 PowerShell 在 SharePoint 中创建内容类型?


SharePoint 中的内容类型是任何项目(例如任务、文档、事件等)的基本框架。每种内容类型都有自己的一组元数据列和模板、信息管理策略和工作流。在 SharePoint 中,我们可以根据现有内容类型创建自己的自定义内容类型,例如基于文档内容类型的“发票”内容类型,并向其中添加自定义列,例如“发票日期”。

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

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

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

    [玩转系统] 如何使用 PowerShell 在 SharePoint 中创建内容类型?

  3. 为您的自定义内容类型提供名称。或者,您可以输入新内容类型的说明以明确其目的。
  4. 选择父内容类型,例如您的内容类型所基于的“项目”。您可以创建新的内容类型组或选择任何现有的内容类型组。

    [玩转系统] 如何使用 PowerShell 在 SharePoint 中创建内容类型?

  5. 单击“确定”完成内容类型的创建。创建内容类型后,下一步是将所需的网站栏添加到内容类型,以使元数据可从内容类型获取。
内容类型是分层的 - 因此,当您在根网站级别创建内容类型时,其下面的所有子网站都会从父网站继承网站栏和内容类型!

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

让我们使用 PowerShell 在 SharePoint 2016 中创建一个新的内容类型:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Parameters
$SiteURL = "https://intranet.crescent.com/"
$ContentTypeName = "Crescent Projects"

#Get the Root web
$web = Get-SPWeb $SiteURL

#Get parent content type - "Item"
$ParentContentType = $web.AvailableContentTypes["Item"]

#Check if the content type already exists
$ContentType = $web.ContentTypes[$ContentTypeName]
If($ContentType -eq $NULL)
{
    #Create new content type from  "item" Content Type
    $ContentType = New-Object Microsoft.SharePoint.SPContentType($ParentContentType, $web.ContentTypes, $ContentTypeName)
    $ContentType.Description ="Crescent Projects Content Type"
    $ContentType.Group = "Crescent Content Types"
    $web.ContentTypes.Add($ContentType)
    $web.Update()
    Write-Host "Content Type '$ContentTypeName' Created Successfully!" -ForegroundColor Green
}
else
{
    Write-Host "Content Type '$ContentTypeName' Already Exists!" -ForegroundColor Red
}

此 PowerShell 在 SharePoint 中创建新的内容类型。现在,下一步是向其中添加一些列:让我们向内容类型添加“项目名称”列。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Parameters
$SiteURL = "https://intranet.crescent.com/"
$ContentTypeName = "Crescent Projects"

#Get the Root web
$web = Get-SPWeb $SiteURL

#Get the Content Type
$ContentType = $web.ContentTypes[$ContentTypeName]

#Check if the content type exists
If($ContentType -ne $NULL)
{
    #Create new Site column
    $FieldName="ProjectName"

    #Check if field exists already
    $Field = $web.Fields | where {$_.InternalName -eq $FieldName}
    If($Field -eq $null)
    {
        #Add a Site Column
        $Field = $Web.Fields.Add($FieldName, [Microsoft.SharePoint.SPFieldType]::Text, $false)
    }
    
    #Get a Field Link to Add the site column to content type
    $Field=$web.Fields[$FieldName]
    $FieldLink = new-object Microsoft.SharePoint.SPFieldLink($Field)
    #Add Site column to content type
    $contentType.Fieldlinks.add($FieldLink)
    $contentType.Update()

    Write-Host "Fied Added to Content Type '$ContentTypeName' Successfully!" -ForegroundColor Green
}
else
{
    Write-Host "Content Type '$ContentTypeName' Doesn't Exist!" -ForegroundColor Red
}

要使用 PowerShell 在 SharePoint Online 中创建内容类型,请参阅:如何使用 PowerShell 在 SharePoint Online 中创建内容类型?

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

取消回复欢迎 发表评论:

关灯