ค้นหาเว็บไซต์

SharePoint Online: การเพิ่มคำศัพท์ ชุดคำศัพท์ และกลุ่มคำศัพท์จำนวนมากจาก CSV โดยใช้ PowerShell


ข้อกำหนด: เพิ่มข้อกำหนด ชุดข้อกำหนด และกลุ่มข้อกำหนดจำนวนมากจากไฟล์ CSV ลงในที่เก็บคำของ SharePoint Online

นี่คือรูปแบบและข้อมูลที่จะนำเข้า:

สคริปต์ PowerShell เพื่อเพิ่มข้อกำหนด ชุดคำศัพท์ และกลุ่มจำนวนมากไปยังที่เก็บคำของ SharePoint Online:

หากคุณมีอนุกรมวิธานจำนวนมากที่จะเพิ่มลงในที่เก็บ Term ของคุณ คุณสามารถใช้ PowerShell เพื่อทำให้กระบวนการเป็นแบบอัตโนมัติได้ ในบทความนี้ เราจะดูวิธีการเพิ่มคำศัพท์ ชุดคำศัพท์ และกลุ่มคำศัพท์จำนวนมากจากไฟล์ CSV โดยใช้ PowerShell ขั้นแรก คุณต้องเตรียมไฟล์ CSV ที่มีข้อมูลคำศัพท์ ชุดคำศัพท์ และกลุ่มคำศัพท์ในรูปแบบข้างต้น บันทึกไฟล์ CSV ในตำแหน่งที่สามารถเข้าถึงได้จากคอมพิวเตอร์ที่คุณจะเรียกใช้สคริปต์ PowerShell


##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"
  
#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 เพื่อนำเข้าข้อมูลที่เก็บคำไปยัง SharePoint Online จากไฟล์ CSV นี่คือสคริปต์ PowerShell เพื่อนำเข้ากลุ่มคำ ชุดคำ และข้อกำหนดจากไฟล์ CSV


#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. ตรวจสอบที่เก็บคำเพื่อดูว่าคำที่กำหนดมีอยู่แล้วในกลุ่มคำเฉพาะและชุดคำ ถ้าไม่ ให้เพิ่มคำที่มีชื่อที่ระบุในคอลัมน์ "Term" ให้กับชุดคำโดยใช้ New-PnPTerm cmdlet

นี่คือเทมเพลต CSV ของฉัน:

บทความที่เกี่ยวข้อง