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

SharePoint Online: ส่งออกข้อมูลที่เก็บคำเป็น CSV โดยใช้ PowerShell


ข้อกำหนด: ส่งออกข้อมูลที่เก็บคำจาก SharePoint Online เป็นไฟล์ CSV

จะส่งออกที่เก็บคำใน SharePoint Online ได้อย่างไร

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

PowerShell เพื่อส่งออกข้อมูลที่เก็บคำเป็น CSV:

มาส่งออกที่เก็บคำศัพท์ใน SharePoint Online โดยใช้ 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/"
$ReportOutput="C:\Temp\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

     #Array to Hold Result - PSObjects
    $ResultCollection = @()

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

    #Get all term groups   
    $TermGroups = $TermStore.Groups
    $Ctx.Load($TermGroups)
    $Ctx.ExecuteQuery()

    #Iterate through each term group
    Foreach($Group in $TermGroups)
    {
        #Get all Term sets in the Term group
        $TermSets = $Group.TermSets
        $Ctx.Load($TermSets)
        $Ctx.ExecuteQuery()

        #Iterate through each termset
        Foreach($TermSet in $TermSets)
        {
            #Get all Terms from the term set
            $Terms = $TermSet.Terms
            $Ctx.Load($Terms)
            $Ctx.ExecuteQuery()

            #Iterate through each term
            Foreach($Term in $Terms)
            {
                $TermData = new-object PSObject
                $TermData | Add-member -membertype NoteProperty -name "Group" -Value $Group.Name
                $TermData | Add-member -membertype NoteProperty -name "TermSet" -Value $Termset.Name
                $TermData | Add-member -membertype NoteProperty -name "Term" -Value $Term.Name   
                $ResultCollection += $TermData
            }
        }
    }
    #export term store sharepoint online powershell
    $ResultCollection | Export-csv $ReportOutput -notypeinformation

    Write-host "Term Store Data Successfully Exported!" -ForegroundColor Green   
}
Catch {
    write-host -f Red "Error Exporting Termstore Data!" $_.Exception.Message
}

สิ่งนี้จะสร้างไฟล์ CSV ดังที่แสดงด้านล่าง: โดยจะส่งออกคำศัพท์ระดับแรกทั้งหมดจากที่เก็บคำ

ส่งออกข้อกำหนดทั้งหมดจากทุกระดับจาก Term Store เป็น CSV:

จะเกิดอะไรขึ้นหากเงื่อนไขใน Term store มีลูกถึง 7 ระดับ?

สคริปต์ PowerShell นี้จะส่งออกคำศัพท์ทั้งหมดจากทุกระดับเป็น CSV คำศัพท์จะคั่นด้วยเครื่องหมายอัฒภาค (;)


#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/"
$ReportOutput="C:\Temp\TermStoreData.csv"

#Function to get all child terms from a Term
Function Get-SPOChildTerms([Microsoft.SharePoint.Client.Taxonomy.Term]$Term)
{
    $TermsColl = @()
    $TermsColl += $Term.PathOfTerm

    #Get All child terms
    $ChildTerms = $Term.Terms
    $Ctx.Load($ChildTerms)
    $Ctx.ExecuteQuery()
 
    #Process all child terms
    Foreach ($ChildTerm in $ChildTerms)
    {
        Get-SPOChildTerms($ChildTerm)
    }
    Return $TermsColl
}

Try {
    #Get Credentials to connect
    $Cred = Get-Credential
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
     #Array to Hold Result - PSObjects
    $ResultCollection = @()
 
    #Get the term store
    $TaxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx) 
    $TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
    $Ctx.Load($TaxonomySession)
    $Ctx.Load($TermStore)
    $Ctx.ExecuteQuery()
 
    #Get all term groups   
    $TermGroups = $TermStore.Groups
    $Ctx.Load($TermGroups)
    $Ctx.ExecuteQuery()
 
    #Iterate through each term group
    Foreach($Group in $TermGroups)
    {
        #Get all Term sets in the Term group
        $TermSets = $Group.TermSets
        $Ctx.Load($TermSets)
        $Ctx.ExecuteQuery()
 
        #Iterate through each termset
        Foreach($TermSet in $TermSets)
        {
            Write-host "Getting Terms from Term Set '$($Termset.Name)' from Term Group '$($Group.Name)'" -f Yellow
            #Get all Terms from the term set
            $Terms = $TermSet.Terms
            $Ctx.Load($Terms)
            $Ctx.ExecuteQuery()

            #Iterate through each term
            Foreach($Term in $Terms)
            {
                #Get the child terms of the term
                $ChildTerms = Get-SPOChildTerms($Term)

                ForEach($Child in $ChildTerms)
                {
                    $TermData = new-object PSObject
                    $TermData | Add-member -membertype NoteProperty -name "Group" -Value $Group.Name
                    $TermData | Add-member -membertype NoteProperty -name "TermSet" -Value $Termset.Name              
                    $TermData | Add-member -membertype NoteProperty -name "Terms" -Value $Child
                    $ResultCollection += $TermData
                }
            }
        }
    }
    $ResultCollection
    #Export Results to a CSV File
    $ResultCollection | Export-csv $ReportOutput -NoTypeInformation -Force
 
    Write-host "Term Store Data Successfully Exported!" -ForegroundColor Green   
}
Catch {
    write-host -f Red "Error Exporting Termstore Data!" $_.Exception.Message
}

ถ้าคุณต้องการส่งออกชุดคำทั้งหมดไปยัง CSV ที่เข้ากันได้กับ SharePoint Online (เพื่อให้คุณสามารถใช้ฟีเจอร์ "นำเข้า" จากที่เก็บคำ) ให้ใช้สคริปต์อื่น: SharePoint Online: ส่งออกชุดคำเป็น CSV โดยใช้ PowerShell

PnP PowerShell เพื่อส่งออกข้อมูลที่เก็บคำไปยัง XML

ต่อไปนี้เป็นวิธีการส่งออกที่เก็บคำใน SharePoint Online โดยใช้ PnP PowerShell cmdlet Export-PnPTermGroupToXml:


#Config Variables
$AdminCenterURL = "https://Crescent-admin.sharepoint.com"

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

#Export Term Store Data to XML
Export-PnPTermGroupToXml -Out "C:\Temp\TermStoreData.xml"

หากต้องการนำเข้าข้อมูล CSV นี้ไปยังที่เก็บคำอื่น ให้ใช้บทความอื่นของฉัน: PowerShell เพื่อนำเข้าข้อมูลที่เก็บคำจาก CSV ใน SharePoint Online

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