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

[玩转系统] SharePoint Online:使用 PowerShell 创建托管元数据网站列

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

SharePoint Online:使用 PowerShell 创建托管元数据网站列


要求:使用 PowerShell 在 SharePoint Online 中添加托管元数据网站列。

如何在 SharePoint Online 中创建托管元数据网站列?

托管元数据是 SharePoint 中的一项功能,允许用户创建和管理受控术语词汇表,以对内容进行组织和分类。 SharePoint 中的网站栏提供了一种可重用的方式来存储元数据,并可用于定义内容类型和列表的结构。在本文中,我们将探讨如何使用 PowerShell 在 SharePoint 中创建托管元数据网站栏。

SharePoint Online 中的托管元数据列使用术语库中的术语集作为其允许值的来源。术语集定义一组值。例如,“Regions”是一个术语集,“Asia”是一个术语。因此,当术语集“区域”用作托管元数据网站列的源时,术语“亚洲”可以作为其值。下面介绍了如何将托管元数据网站列添加到 SharePoint Online 列表。

  1. 浏览到您的 SharePoint Online 网站并导航到网站设置 >> 网站栏
  2. 单击页面顶部的“创建”链接
  3. 为您的网站列提供名称,将字段类型指定为“托管元数据”

    [玩转系统] SharePoint Online:使用 PowerShell 创建托管元数据网站列

  4. 向下滚动并从术语库中选择适当的术语集。

    [玩转系统] SharePoint Online:使用 PowerShell 创建托管元数据网站列

  5. 填写其他可选值,然后单击“确定”以在 SharePoint Online 中创建托管元数据网站列。

PowerShell 在 SharePoint Online 中创建托管元数据网站列

以下是在 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"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"

#Custom function to add Managed Metadata Site Column
Function Add-SPOManagedMetadataSiteColumn()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ColumnName,        
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$false)] [string] $Description = [string]::Empty,
        [Parameter(Mandatory=$false)] [string] $IsRequired = $False,
        [Parameter(Mandatory=$false)] [string] $EnforceUniqueValues = $False,
        [Parameter(Mandatory=$true)] [string] $ColumnGroup,
        [Parameter(Mandatory=$true)] [string] $TermGroupName,
        [Parameter(Mandatory=$true)] [string] $TermSetName
    )
 
    #Generate new GUID for Field ID
    $FieldID = ([GUID]::NewGuid()).GUID
 
    Try {
        $Cred= Get-Credential
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Get all Site columns from the site
        $Fields = $Ctx.web.Fields
        $Ctx.Load($Fields)
        $Ctx.executeQuery()
 
        #Check if the column name exists
        $NewField = $Fields | where {$_.Title -eq $DisplayName -or $_.InternalName -eq $ColumnName} 
        if($NewField -ne $NULL)  
        {
            Write-host "Site Column $ColumnName already exists!" -f Yellow
        }
        else
        {
            #Create Managed Metadata Column
            $FieldSchema = "<Field Type='TaxonomyFieldType' ID='{$FieldID}' DisplayName='$DisplayName' Name='$ColumnName' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' Group='$ColumnGroup'/>"
            $NewField = $Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
            $Ctx.ExecuteQuery()    
 
            Write-host "Site Column Created Successfully!" -ForegroundColor Green  

            #Get the Termset to Bind
            $TaxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx) 
            $TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
            $Ctx.Load($TaxonomySession)
            $Ctx.Load($TermStore)
            $Ctx.ExecuteQuery() 
            #Get the Term Group
            $TermGroup=$TermStore.Groups.GetByName($TermGroupName)
            #Get the term set
            $TermSet = $TermGroup.TermSets.GetByName($TermSetName)
            $Ctx.Load($TermSet)
            $Ctx.ExecuteQuery() 
 
            #Bind Managed Metadata Termset to the column
            $TaxField = [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo").MakeGenericMethod([Microsoft.SharePoint.Client.Taxonomy.TaxonomyField]).Invoke($Ctx, $NewField)
            $TaxField.SspId = $TermStore.Id
            $TaxField.TermSetId = $TermSet.Id
            $TaxField.Update()
            $Ctx.ExecuteQuery()
 
            Write-host "Site Column Binded to the Termset Successfully!" -ForegroundColor Green
        }
    }
    Catch {
        write-host -f Red "Error Creating Site Column!" $_.Exception.Message
    }
}
 
#Define value for parameters
$SiteURL="https://Crescent.sharepoint.com"
$ColumnName="ProjectRegion"
$DisplayName="Project Regions"
$ColumnGroup="Crescent Projects"
$TermGroupName="Deals Pipeline"
$TermSetName="Regions"
 
#Call the function to create Managed Metadata Site Column
Add-SPOManagedMetadataSiteColumn -SiteURL $SiteURL -ColumnName $ColumnName -DisplayName $DisplayName -ColumnGroup $ColumnGroup -TermGroupName $TermGroupName -TermSetName $TermSetName

此脚本创建一个新的网站栏“项目区域”,并映射到术语库中“交易”组下的术语集“区域”。

PnP PowerShell 在 SharePoint Online 中添加托管元数据网站列

以下是如何使用 PnP PowerShell 添加托管元数据字段:


#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"

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

#Create a Managed Metadata column from "Regions" term set in "Deals Pipeline" Group
Add-PnPTaxonomyField -DisplayName "Region" -InternalName "Region" -TermSetPath "Deals Pipeline|Regions"

包起来

总之,在 SharePoint 中创建托管元数据网站栏是以一致且受控的方式存储和管理元数据的有效方法。使用 PowerShell 创建这些网站栏是一种快速有效的方法,有助于简化流程并避免手动错误。

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

取消回复欢迎 发表评论:

关灯