SharePoint Online: obtenga vistas de lista usando PowerShell
Requisito: Obtener vistas de lista de SharePoint Online mediante PowerShell.
PowerShell para obtener todas las vistas de una lista:
Esta publicación de blog le mostrará cómo obtener todas las vistas de lista de su lista de SharePoint Online usando 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 Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$ListName="Projects"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the List
$List=$Ctx.Web.Lists.GetByTitle($ListName)
#$Ctx.Load($List)
#Get All List Views of the List
$ctx.Load($List.Views)
$Ctx.ExecuteQuery()
Write-host "Total Views in the List:"$List.Views.Count
#Iterate through each View
ForEach($View in $List.Views)
{
#Get the View
$ctx.Load($View)
$Ctx.ExecuteQuery()
Write-Host -f Yellow $View.Title
}
}
Catch {
write-host -f Red "Error Getting List Views!" $_.Exception.Message
}
Obtenga la vista predeterminada de la lista:
#Get the Default View of the List
$DefaultView = $List.DefaultView
$Ctx.Load($DefaultView)
$Ctx.ExecuteQuery()
Write-Host $DefaultView.Title
Obtener una vista específica de la lista
Este script de PowerShell obtiene una vista específica de una lista o biblioteca de SharePoint Online:
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$ListName="Projects"
$ViewTitle ="All Items"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the List
$List=$Ctx.Web.Lists.GetByTitle($ListName)
#Get the List View
$ListView = $List.views.GetByTitle($ViewTitle)
$Ctx.load($ListView)
$Ctx.executeQuery()
#Get the View URL
Write-host "URL of the List View:"$ListView.ServerRelativeUrl
}
Catch {
write-host -f Red "Error Getting List Views!" $_.Exception.Message
}
Obtener todas las vistas de una lista usando PnP PowerShell
Podemos obtener una lista de vistas disponibles para una lista específica utilizando el cmdlet PnP PowerShell Get-PnPView:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName= "Projects"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
#Get All List Views from the list
$ListViews = Get-PnPView -List $ListName
#Iterate through each View
ForEach($View in $ListViews)
{
Write-host $View.Title
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
De manera similar, para obtener una vista específica usando PnP PowerShell, use:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName= "Projects"
$ViewName= "Active Projects"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
#Get the List View from the list
$ListView = Get-PnPView -List $ListName -Identity $ViewName -ErrorAction Stop
#Get All Properties of the View
$ListView | Select-Object -Property *
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}