Tìm kiếm trang web

SharePoint Online: Nhận tất cả các mục từ Chế độ xem danh sách bằng PowerShell


Yêu cầu: Nhận tất cả các mục từ Chế độ xem danh sách SharePoint Online trong PowerShell.

PowerShell để lấy các mục danh sách từ dạng xem trong SharePoint Online

Trong SharePoint Online, bạn có thể sử dụng PowerShell để lấy danh sách các mục từ một dạng xem. Điều này có thể hữu ích nếu bạn muốn tự động hóa các tác vụ dựa trên dữ liệu trong dạng xem. Bài viết này sẽ chỉ cho bạn cách sử dụng truy vấn CAML và tập lệnh PowerShell để lấy danh sách các mục từ một dạng xem.


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

Điều này có thể hữu ích nếu bạn cần xuất dữ liệu từ chế độ xem hoặc nếu bạn cần khắc phục sự cố với chế độ xem. Nhưng có một vấn đề! Nếu chế độ xem của bạn được phân trang và định cấu hình để hiển thị các mục theo lô, tập lệnh trên chỉ cung cấp cho bạn các mục từ trang đầu tiên! Vì vậy, đây là cách khắc phục:


#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 để lấy các mục từ chế độ xem danh sách

Dưới đây là cách lấy tất cả các mục từ chế độ xem danh sách SharePoint bằng 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

Bằng cách làm theo các bước được nêu trong bài viết này, bạn có thể dễ dàng truy xuất các mục từ dạng xem và sử dụng PowerShell để thao tác và phân tích dữ liệu nếu cần. Để lấy tất cả các trường từ một dạng xem, hãy sử dụng: Nhận tất cả các trường từ dạng xem danh sách SharePoint Online bằng PowerShell

Bài viết liên quan