SharePoint Online: รับมุมมองรายการโดยใช้ PowerShell
ความต้องการ: รับมุมมองรายการ SharePoint Online โดยใช้ PowerShell
PowerShell เพื่อรับมุมมองทั้งหมดจากรายการ:
โพสต์ในบล็อกนี้จะแสดงวิธีรับมุมมองรายการทั้งหมดจากรายการ 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"
#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
}
รับมุมมองเริ่มต้นของรายการ:
#Get the Default View of the List
$DefaultView = $List.DefaultView
$Ctx.Load($DefaultView)
$Ctx.ExecuteQuery()
Write-Host $DefaultView.Title
รับมุมมองเฉพาะจากรายการ
สคริปต์ PowerShell นี้ได้รับมุมมองเฉพาะจากรายการหรือไลบรารี 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
}
รับมุมมองทั้งหมดจากรายการโดยใช้ PnP PowerShell
เราสามารถรับรายการมุมมองที่มีอยู่สำหรับรายการเฉพาะได้โดยใช้ PnP PowerShell cmdlet 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
}
ในทำนองเดียวกัน หากต้องการรับมุมมองเฉพาะโดยใช้ PnP PowerShell ให้ใช้:
#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
}