当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 获取列表视图

作者:精品下载站 日期:2024-12-14 14:40:06 浏览:13 分类:玩电脑

SharePoint Online:使用 PowerShell 获取列表视图


要求:使用 PowerShell 获取 SharePoint Online 列表视图。

PowerShell 从列表中获取所有视图:

这篇博文将向您展示如何使用 PowerShell 从 SharePoint Online 列表中获取所有列表视图。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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
}

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯