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

[玩转系统] SharePoint Online:使用 PowerShell 将托管元数据列添加到列表

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

SharePoint Online:使用 PowerShell 将托管元数据列添加到列表


要求:在 SharePoint Online 中创建托管元数据列。

SharePoint Online:如何将托管元数据字段添加到列表或库

SharePoint 中的托管元数据使用术语库中的术语集作为其允许值的来源。术语集定义一组值。例如,部门是一个术语集,“销售”是一个术语。因此,当术语集“部门”用作托管元数据列的源时,可以选择术语“销售”作为其值。下面介绍了如何将托管元数据列添加到 SharePoint Online 列表。

  1. 浏览到 SharePoint Online 网站并导航到要添加托管元数据列的目标列表。
  2. 在“列表”选项卡下,单击功能区中的“创建列”按钮。
  3. 为新列提供名称,将字段类型指定为“托管元数据”

    [玩转系统] SharePoint Online:使用 PowerShell 将托管元数据列添加到列表

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

    [玩转系统] SharePoint Online:使用 PowerShell 将托管元数据列添加到列表

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

PowerShell 在 SharePoint Online 列表中创建托管元数据列:

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

#Custom function to add column to list
Function Add-ManagedMetadataColumnToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $Name,
        [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] $TermGroupName,
        [Parameter(Mandatory=$true)] [string] $TermSetName
    )

    #Generate new GUID for Field ID
    $FieldID = New-Guid

    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 the web, List and Lookup list
        $Web = $Ctx.web
        $List = $Web.Lists.GetByTitle($ListName)
        $LookupList = $Web.Lists.GetByTitle($LookupListName)
        $Ctx.Load($Web)
        $Ctx.Load($List)
        $Ctx.Load($LookupList)
        $Ctx.ExecuteQuery()

        #Check if the column exists in list already
        $Fields = $List.Fields
        $Ctx.Load($Fields)
        $Ctx.executeQuery()
        $NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
        if($NewField -ne $NULL)  
        {
            Write-host "Column $Name already exists in the List!" -f Yellow
        }
        else
        {
            #Get the Term set data
            $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()
            
            #Create Managed Metadata Column
            $FieldSchema = "<Field Type='TaxonomyFieldType' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' />"
            $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
            $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 "New Column Added to the List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error Adding Column to List!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Name="ProjectRegions"
$DisplayName="Project Regions"
$Description="Select the Project Region"
$TermGroupName="Regions"
$TermSetName="MENA"

#Call the function to add column to list
Add-ManagedMetadataColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description -TermGroupName $TermGroupName -TermSetName $TermSetName

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

下面是使用 Add-PnPTaxonomyField cmdlet 在 SharePoint Online 列表中创建新 MMS 列的 PnP PowerShell。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName = "Projects"

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

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

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

取消回复欢迎 发表评论:

关灯