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

SharePoint Online: รับรายการทั้งหมดจากมุมมองรายการโดยใช้ PowerShell


ความต้องการ: รับรายการทั้งหมดจากมุมมองรายการ SharePoint Online ใน PowerShell

PowerShell เพื่อรับรายการจากมุมมองใน SharePoint Online

ใน SharePoint Online คุณสามารถใช้ PowerShell เพื่อรับรายการข้อมูลจากมุมมองได้ สิ่งนี้มีประโยชน์ถ้าคุณต้องการทำงานที่ยึดตามข้อมูลในมุมมองโดยอัตโนมัติ บทความนี้จะแสดงวิธีใช้แบบสอบถาม CAML และสคริปต์ 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"

#Config Variables
$SiteURL="https://crescent.sharepoint.com/sites/pmo"
$ListName="Projects"
$ViewName="Active Projects"

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 View
    $List = $Ctx.web.Lists.GetByTitle($ListName)
    $View = $List.Views.GetByTitle($ViewName)
    $Ctx.Load($View)
    $Ctx.ExecuteQuery()
 
    #Get all list items from the view
    $CAMLQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
    $CAMLQuery.ViewXml = $View.ListViewXml
    $ListItems = $List.GetItems($CAMLQuery)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    #Iterate throgh each item: 
    #ForEach($Item in $ListItems) { Write-host $Item[ColumnName]}
     
    Write-host "Total List Items Found in the Given View: $($ListItems.Count)" -ForegroundColor Green  
}
Catch {
    write-host -f Red "Error Getting List Items from the List View!" $_.Exception.Message
} 

สิ่งนี้มีประโยชน์หากคุณต้องการส่งออกข้อมูลจากมุมมองหรือหากคุณต้องการแก้ไขปัญหาเกี่ยวกับมุมมอง แต่มีปัญหา! หากมุมมองของคุณถูกเพจและกำหนดค่าให้แสดงรายการเป็นชุด สคริปต์ด้านบนจะทำให้คุณได้รับเฉพาะรายการจากหน้าที่ 1 เท่านั้น! ดังนั้นนี่คือการแก้ไข:


#Config Variables
$SiteURL="https://crescentintranet.sharepoint.com/sites/PMO"
$ListName="Projects"
$ViewName="Active Projects"
 
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 View
    $List = $Ctx.web.Lists.GetByTitle($ListName)
    $View = $List.Views.GetByTitle($ViewName)
    $Ctx.Load($View)
    $Ctx.ExecuteQuery()

    #Get the View Query
    $View.Retrieve("ViewQuery")
    $Ctx.ExecuteQuery()

    #Get all list items from the view
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "<View Scope='RecursiveAll'><Query>$($View.ViewQuery)</Query></View>"

    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    Write-host "Total List Items Found in the Given View: $($ListItems.Count)" -ForegroundColor Green   
    #Loop through each List item
    ForEach($Item in $ListItems)
    {
        Write-host $Item.Id  #$Item["Title"]
    }
}
Catch {
    write-host -f Red "Error Getting List Items from the List View!" $_.Exception.Message
}

PnP PowerShell เพื่อรับรายการจากมุมมองรายการ

ต่อไปนี้เป็นวิธีรับรายการทั้งหมดจากมุมมองรายการ SharePoint โดยใช้ PnP PowerShell:


#Parameters
$SiteURL="https://crescent.sharepoint.com/sites/PMO"
$ListName="Projects"
$ViewName= "Active Projects"

#Connect to the Site        
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the List
$List =  Get-PnPList -Identity $ListName

#Get the List View from the list
$ListView  = Get-PnPView -List $ListName -Identity $ViewName -Includes ListViewXml

#Get List Items from the view
Get-PnPListItem -List $ListName -Query $ListView.ListViewXml

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

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