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

[玩转系统] SharePoint Online:使用 PowerShell 在术语存储中创建子术语

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

SharePoint Online:使用 PowerShell 在术语存储中创建子术语


要求:在 SharePoint Online 术语存储中创建子术语

如何在 SharePoint Online 中添加子条款?

在 SharePoint Online 中,您可以向术语集中的术语添加最多 7 级的子术语,以创建术语的分层组织或将术语分解为特定类别。有几种不同的方法可以将子术语添加到术语中 - 使用术语库管理器或 PowerShell cmdlet。在这篇博文中,我们将研究每个选项,并向您展示如何使用每种方法添加子术语。

SharePoint Online 术语存储提供了一个简单的界面来分层管理术语存储数据。我们可以在术语库中的现有术语上创建子术语(或子术语)。

  • 您可以通过从任何现有术语的上下文菜单中选择“添加术语”,在术语库中的任何现有术语上创建子术语。

    [玩转系统] SharePoint Online:使用 PowerShell 在术语存储中创建子术语

PowerShell 在 SharePoint Online 中添加子术语

以下是在 SharePoint Online 中创建子术语的 PowerShell 方法:


#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"

#Parameters
$TenantURL = "https://crescent.sharepoint.com"
$TermGroupName = "Classification"
$TermSetName = "Tags"
$ParentTermName = "Continent"

#Function to Add Child Term (or Sub-term)
Function Add-ChildTerm
{
    param (
    [parameter(Mandatory=$true, ValueFromPipeline=$true)][string]$TermName,
    [parameter(ValueFromPipelineByPropertyName = $true)][int]$Language = 1033,
    [parameter(Mandatory=$true, ValueFromPipeline=$true)][Microsoft.SharePoint.Client.Taxonomy.Term]$ParentTerm,
    [parameter(Mandatory=$true, ValueFromPipeline=$true)][Microsoft.SharePoint.Client.ClientContext]$Context
    )
    Try {
        $ChildTerm = $ParentTerm.CreateTerm($TermName, $Language, [System.Guid]::NewGuid().toString())
        $ParentTerm.TermStore.CommitAll()
        $Context.ExecuteQuery()
        Write-host "New Term '$TermName' Added Successfully!" -ForegroundColor Green
    }
    Catch {
        write-host -f Red "Error Adding Term:" $_.Exception.Message
    }
}

Try {
    #Get Credentials to connect
    $Cred = Get-Credential
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($TenantURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #Get the term store
    $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)
    $Ctx.Load($TermGroup)
 
    #Get the Term Set
    $TermSet = $TermGroup.TermSets.GetByName($TermSetName)
    $Ctx.Load($TermSet)
 
    #Get the Parent term
    $Term = $TermSet.Terms.GetByName($ParentTermName)
    $Ctx.Load($Term)
    $Ctx.ExecuteQuery()
     
    #Call the function to Create a Child Term
    Add-ChildTerm -TermName "America" -ParentTerm $Term -Context $Ctx
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

使用 PnP PowerShell 在 SharePoint Online 中创建子条款

我们还可以使用 PnP PowerShell 创建子术语:


#Parameters
$TenantURL = "https://crescent.sharepoint.com" 

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $TenantURL -Interactive
    
    $ParentTerm = Get-PnPTerm -TermGroup "Classification" -TermSet "Tags" -Identity "Continent" -IncludeChildTerms -Recursive
    $ChildTerm = $ParentTerm.CreateTerm("Asia", 1033, (New-Guid))
    Invoke-PnPQuery    
} 
Catch {
    Write-Host -f Red "Error:"$_.Exception.Message
}

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

取消回复欢迎 发表评论:

关灯