Búsqueda de sitios web

SharePoint Online: obtenga todos los elementos de una vista de lista usando PowerShell


Requisito: Obtener todos los elementos de una vista de lista de SharePoint Online en PowerShell.

PowerShell para obtener elementos de lista desde una vista en SharePoint Online

En SharePoint Online, puede usar PowerShell para obtener una lista de elementos de una vista. Esto puede resultar útil si desea automatizar tareas basadas en los datos de una vista. Este artículo le mostrará cómo utilizar la consulta CAML y el script de PowerShell para obtener una lista de elementos de una vista.


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

Esto puede resultar útil si necesita exportar datos desde una vista o si necesita solucionar problemas con la vista. ¡Pero hay un problema! Si su vista está paginada y configurada para mostrar elementos en lotes, el script anterior solo le proporciona elementos de la primera página. Entonces, aquí está la solución:


#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 para obtener elementos de una vista de lista

A continuación se explica cómo obtener todos los elementos de una vista de lista de SharePoint usando 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

Si sigue los pasos descritos en este artículo, puede recuperar fácilmente los elementos de una vista y usar PowerShell para manipular y analizar los datos según sea necesario. Para obtener todos los campos de una vista, use: Obtener todos los campos de una vista de lista de SharePoint Online usando PowerShell

Artículos relacionados