Tìm kiếm trang web

SharePoint Online: Thêm cột siêu dữ liệu được quản lý vào danh sách bằng PowerShell


Yêu cầu: Tạo cột siêu dữ liệu được quản lý trong SharePoint Online.

SharePoint Online: Cách thêm trường siêu dữ liệu được quản lý vào danh sách hoặc thư viện

Siêu dữ liệu được quản lý trong SharePoint sử dụng Tập hợp thuật ngữ từ kho thuật ngữ làm nguồn cho các giá trị được phép của nó. Một tập hợp thuật ngữ xác định một tập hợp các giá trị. Ví dụ: Bộ phận là một Bộ thuật ngữ và “Bán hàng” là một Thuật ngữ. Vì vậy, khi tập hợp thuật ngữ “Bộ phận” được sử dụng làm nguồn cho cột Siêu dữ liệu được quản lý thì thuật ngữ “Bán hàng” có thể được chọn làm giá trị của nó. Đây là cách thêm cột siêu dữ liệu được quản lý vào danh sách SharePoint Online.

  1. Duyệt đến trang SharePoint Online của bạn và điều hướng đến danh sách mục tiêu nơi bạn muốn thêm cột Siêu dữ liệu được quản lý.
  2. Trong tab Danh sách, nhấp vào nút “Tạo cột” trong ruy-băng.
  3. Cung cấp Tên cho cột mới của bạn, chỉ định loại trường là “Siêu dữ liệu được quản lý”

  4. Cuộn xuống và chọn Tập hợp thuật ngữ hoặc Thuật ngữ thích hợp từ cửa hàng phân loại.

  5. Điền vào các giá trị tùy chọn khác và nhấp vào “OK” để tạo trường Siêu dữ liệu được quản lý trong danh sách SharePoint Online.

PowerShell để tạo cột Siêu dữ liệu được quản lý trong Danh sách SharePoint Online:

Sử dụng PowerShell, hãy thêm cột siêu dữ liệu được quản lý vào danh sách SharePoint Online.


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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 để thêm cột siêu dữ liệu được quản lý trong SharePoint Online

Đây là PnP PowerShell để tạo cột MMS mới trong danh sách SharePoint Online bằng lệnh ghép ngắn Add-PnPTaxonomyField.


#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

Bài viết liên quan