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

[玩转系统] SharePoint Online:使用 PowerShell 从列表视图中获取所有项目

作者:精品下载站 日期:2024-12-14 14:52:02 浏览:17 分类:玩电脑

SharePoint Online:使用 PowerShell 从列表视图中获取所有项目


要求:从 PowerShell 中的 SharePoint Online 列表视图获取所有项目。

[玩转系统] SharePoint Online:使用 PowerShell 从列表视图中获取所有项目

PowerShell 从 SharePoint Online 中的视图获取列表项

在 SharePoint Online 中,您可以使用 PowerShell 从视图中获取项目列表。如果您想要自动执行基于视图中的数据的任务,这会很有用。本文将向您展示如何使用 CAML 查询和 PowerShell 脚本从视图中获取项目列表。


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

如果您需要从视图导出数据或者需要解决视图问题,这会很有用。但有一个问题!如果您的视图已分页并配置为批量显示项目,则上述脚本仅获取第一页中的项目!所以,这是修复方法:


#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 从列表视图中获取项目

以下是如何使用 PnP PowerShell 从 SharePoint 列表视图中获取所有项目:


#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

通过执行本文中概述的步骤,您可以轻松地从视图中检索项目,并根据需要使用 PowerShell 操作和分析数据。要从视图中获取所有字段,请使用:使用 PowerShell 从 SharePoint Online 列表视图中获取所有字段

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

取消回复欢迎 发表评论:

关灯