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

[玩转系统] SharePoint Online:使用 PowerShell 从 CSV 批量添加术语、术语集和术语组

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

SharePoint Online:使用 PowerShell 从 CSV 批量添加术语、术语集和术语组


要求:将 CSV 文件中的术语、术语集和术语组批量添加到 SharePoint Online 术语存储中。

以下是要导入的格式和数据:

[玩转系统] SharePoint Online:使用 PowerShell 从 CSV 批量添加术语、术语集和术语组

用于将术语、术语集和组批量添加到 SharePoint Online 术语库的 PowerShell 脚本:

如果您有大量分类法要添加到术语库中,则可以使用 PowerShell 来自动执行该过程。在本文中,我们将了解如何使用 PowerShell 从 CSV 文件批量添加术语、术语集和术语组。首先,您需要准备一个 CSV 文件,其中包含上述格式的术语、术语集和术语组信息。将 CSV 文件保存在可从运行 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"
  
#Variables for Processing
$AdminURL = "https://crescent-admin.sharepoint.com/"
$CSVFile = "C:\Users\salaudeen\Desktop\TermStoreData.csv"

Try {
    #Get Credentials to connect
    $Cred = Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminURL)
    $Ctx.Credentials = $Credentials

    #Get the term store
    $TaxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx) 
    $TaxonomySession.UpdateCache()
    $TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
    $Ctx.Load($TaxonomySession)
    $Ctx.Load($TermStore)
    $Ctx.ExecuteQuery()

    #Get Termstore data from CSV and iterate through each row
    Import-Csv $CSVFile | ForEach-Object {
        
        $TermGroupName = $_.TermGroup
        $TermSetName = $_.TermSet
        $TermName = $_.Term

        #**** Step 1: Create Term Group ****/
        #Check if the given group exists already
        $TermGroups = $TermStore.Groups
        $Ctx.Load($TermGroups)
        $Ctx.ExecuteQuery()
        $TermGroup = $TermGroups | Where-Object {$_.Name -eq $TermGroupName}
     
        If(-not $TermGroup)
        {
            #Create Term Group
            Write-host "Creating Term Group '$TermGroupName'" -ForegroundColor Cyan
            $TermGroup = $TermStore.CreateGroup($TermGroupName, [System.Guid]::NewGuid().toString())
            $Ctx.Load($TermGroup)
            $Ctx.ExecuteQuery()
            $TermGroup.TermStore.CommitAll()
            $TaxonomySession.UpdateCache()

            Write-host "Term Group '$TermGroupName' Created Successfully!" -ForegroundColor Green
        }
        else
        {
            Write-host "Term Group '$TermGroupName' Exists Already!" -ForegroundColor Yellow
        }


        #**** Step 2: Create Term Set ****#
        #Check if the given term set exists already
        $TermSets = $TermGroup.TermSets
        $Ctx.Load($TermSets)
        $Ctx.ExecuteQuery()
        $TermSet = $TermSets | Where-Object {$_.Name -eq $TermSetName}
     
        If(-not $TermSet)
        {
            #Create Term Set
            Write-host "Creating Term Set '$TermSetName'" -ForegroundColor Cyan
            $TermSet = $TermGroup.CreateTermSet($TermSetName,[System.Guid]::NewGuid().toString(),1033)
            $Ctx.Load($TermSet)
            $Ctx.ExecuteQuery()
            $TermSet.TermStore.CommitAll()
            $TaxonomySession.UpdateCache()
     
            Write-host "Term Set '$TermSetName' Created Successfully!" -ForegroundColor Green
        }
        else
        {
            Write-host "Term Set '$TermSetName' Exists Already!" -ForegroundColor Yellow
        }


        #*** Step 3: Create Term ***#
        #Check if the given term exists already
        $Terms = $TermSet.Terms
        $Ctx.Load($Terms)
        $Ctx.ExecuteQuery()
        $Term = $Terms | Where-Object {$_.Name -eq $TermName}
    
        If(-not $Term)
        {
            #Create Term Set
            Write-host "Creating Term '$TermName'" -ForegroundColor Cyan
            $Term = $TermSet.CreateTerm($TermName,1033,[System.Guid]::NewGuid().toString())
            $Ctx.Load($Term)
            $Ctx.ExecuteQuery()
            $Term.TermStore.CommitAll()
            $TaxonomySession.UpdateCache()
            Write-host "New Term '$TermName' Added Successfully!" -ForegroundColor Green
        }
        else
        {
            Write-host "Term '$TermName' Exists Already!" -ForegroundColor Yellow
        }
    }
 }
Catch {
    write-host -f Red "Error Importing Term store Data!" $_.Exception.Message
} 

PnP PowerShell 从 CSV 导入术语库数据

您可以使用 PnP PowerShell 将术语库数据从 CSV 文件导入到 SharePoint Online。以下是用于从 CSV 文件导入术语组、术语集和术语的 PowerShell 脚本。


#Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$CSVFile = "C:\Temp\TermStoreData.csv"

#Connect to Admin Center
Connect-PnPOnline -Url $AdminCenterURL -Interactive

#Get the Data from CSV
$CSVFile = Import-Csv $CSVFile
$RowCount = $CSVFile.Count
$Counter =1

#Iterate through each row in the CSV file
ForEach ($Row in $CSVFile) 
{
    # Get the term group
    $TermGroup = Get-PnPTermGroup -Identity $Row.TermGroup -ErrorAction SilentlyContinue
    if (!$TermGroup) {
        # Create the term group if it doesn't exist
        $TermGroup = New-PnPTermGroup -GroupName $Row.TermGroup
    }

    # Get the term set
    $TermSet = Get-PnPTermSet -Identity $Row.TermSet -TermGroup $TermGroup -ErrorAction SilentlyContinue
    if (!$TermSet) {
        # Create the term set if it doesn't exist
        $TermSet = New-PnPTermSet -Name $Row.TermSet -TermGroup $TermGroup
    }

    # Get the term
    $Term = Get-PnPTerm -Identity $Row.Term -TermSet $Termset -TermGroup $TermGroup -ErrorAction SilentlyContinue
    if (!$Term) {
        # Create the term if it doesn't exist
        $Term = New-PnPTerm -Name $Row.Term -TermSet $TermSet -TermGroup $TermGroup
    }
    Write-host "Processed Row $Counter out of $RowCount"
    $Counter++
}

上面的 PowerShell 脚本将循环遍历 CSV 文件中的每一行并执行以下操作:

  1. 获取“TermGroup”列中指定名称的术语组。如果术语组不存在,它将使用 New-PnPTermGroup cmdlet 创建一个新术语组。
  2. 获取术语组中“术语集”列中指定名称的术语集。如果术语集不存在,它将使用 New-PnPTermSet cmdlet 创建一个新术语集。
  3. 检查术语库以查看给定术语是否已存在于特定术语组和术语集中。如果没有,请使用 New-PnPTerm cmdlet 将具有“术语”列中指定名称的术语添加到术语集中。

这是我的 CSV 模板:

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

取消回复欢迎 发表评论:

关灯