[玩转系统] SharePoint Online:使用 PowerShell 脚本获取所有列表项
作者:精品下载站 日期:2024-12-14 14:04:13 浏览:11 分类:玩电脑
SharePoint Online:使用 PowerShell 脚本获取所有列表项
要求:SharePoint Online PowerShell 获取列表项。
SharePoint Online:使用 PowerShell 获取所有列表项
PowerShell 是一种功能强大的脚本语言,可用于自动执行任务。在这篇博文中,我们将向您展示如何使用 PowerShell 查询 SharePoint Online 列表中的项目。我们将介绍如何入门的基础知识,并提供一些有关如何在 SharePoint Online 中查询列表项的 PowerShell 示例。让我们开始吧!
如何使用 PowerShell 从 SharePoint Online 列表中获取项目?
您是否正在寻找一种快速、简单的方法来获取 SharePoint Online 中的列表项?好吧,这里是一个示例 PowerShell CSOM 脚本,用于递归地获取给定列表或库中的所有项目,包括所有子文件夹中的项目。
#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"
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com"
$ListName="Projects"
$UserName="[email protected]"
$Password ="Password goes here"
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $credentials
#Get the List
$List = $Context.web.Lists.GetByTitle($ListName)
#sharepoint online get list items powershell
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Context.Load($ListItems)
$Context.ExecuteQuery()
write-host "Total Number of List Items found:"$ListItems.Count
#Loop through each item
$ListItems | ForEach-Object {
#Get the Title field value
write-host $_["Title"]
}
SharePoint Online:PowerShell 按 ID 获取列表项
如果您想通过 ID 获取单个列表项,请使用以下命令:
#Get the List Item
$List = $Context.web.Lists.GetByTitle($ListName)
$ListItem = $List.GetItemById(15)
$Context.Load($ListItem)
$Context.ExecuteQuery()
获取项目后,您可以获得列表项属性,如下所示:
#sharepoint online powershell get list item properties
write-host $ListItem["Title"]
PowerShell 使用 CAML 筛选和获取列表项:
使用 CAML 查询过滤和查询列表项怎么样?以下是如何使用 PowerShell 从 SharePoint Online 列表中获取项目。
SharePoint Online:PowerShell 按标题获取列表项
以下是如何在 SharePoint Online 中使用 PowerShell 获取按 CAML 查询筛选的列表项:
#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/sites/marketing"
$ListName="Project Tasks"
$ItemTitle = "Project Kickoff"
#Setup Credentials to connect
$Cred = Get-Credential
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Get the web and List
$Web=$Ctx.Web
$List=$web.Lists.GetByTitle($ListName)
#Get a List item by its Title
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View>
<Query>
<Where>
<Eq>
<FieldRef Name='Title'/><Value Type='Text'>$ItemTitle</Value>
</Eq>
</Where>
</Query>
</View>"
#Get All List Items matching the query
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
Write-host "Total List Items:" $ListItems.count
#Loop through each file in the library
Foreach($Item in $ListItems)
{
Write-host -f Green $Item["Title"]
}
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
此 PowerShell 在 SharePoint Online 中按标题获取列表项。同样,您可以过滤任何其他数据类型,例如下拉列表等。
#Filter and Get the List Items using CAML
$List = $Context.web.Lists.GetByTitle($ListName)
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery;
$Query.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Priority' /><Value Type='Choice'>(3) High</Value></Eq></Where></Query></View>"
#sharepoint online powershell query list items
$ListItems = $list.GetItems($Query);
$Context.Load($ListItems)
$Context.ExecuteQuery()
#sharepoint online powershell loop through list items
$ListItems | ForEach-Object {
#Get the Title field value
write-host $_["Title"]
}
PowerShell 过滤文件夹并获取所有列表项
让我们从“ListItems”集合中过滤所有文件夹对象并仅获取文件。
#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"
$LibraryName="Project Docs"
#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 web and Library
$Web=$Ctx.Web
$List=$web.Lists.GetByTitle($LibraryName)
#Get all List items from the library - Exclude "Folder" objects
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml="<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType'/><Value Type='Integer'>0</Value></Eq></Where></Query></View>"
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
Write-host "Total List Items:" $ListItems.count
#Loop through each file in the library
Foreach($Item in $ListItems)
{
Write-host -f Green $Item["Title"]
}
}
Catch {
write-host -f Red "Error deleting versions!" $_.Exception.Message
}
SharePoint Online:PnP PowerShell 使用 Get-PnPListItem cmdlet 获取列表项
如何在 PowerShell 中从 SharePoint Online 列表获取项目?下面是使用 Get-PnPListItem cmdlet 从 SharePoint Online 列表获取所有列表项的 PnP PowerShell。确保您的系统中已安装 PnP PowerShell 模块,并且在执行此 PowerShell 脚本之前您有权访问 SharePoint 站点。
#Config Variables
$SiteURL="https://Crescent.sharepoint.com/sites/marketing"
$ListName= "Documents"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#sharepoint online pnp powershell get list items
$ListItems = Get-PnPListItem -List $ListName -Fields "Title" -PageSize 2000
#Loop through each Item
foreach($ListItem in $ListItems)
{
Write-Host "Title:" $ListItem["Title"]
}
此脚本从给定 SharePoint Online 网站集中的“文档”列表中获取所有文件和文件夹作为列表项。在较大的列表中,请确保使用“PageSize”参数。否则,您可能会遇到“Get-PnPListItem:尝试的操作被禁止,因为它超出了列表视图阈值。”错误信息。此外,带有“Query”参数和“Where”条件或“FolderServerRelativeurl”的 Get-PnPListItem cmdlet 在较大的列表上失败。截至目前,“PageSize”对这些参数没有影响。
Get-PnPListItem -List $ListName -Fields "Title" -PageSize 2000
在 SharePoint Online 中使用 PnP PowerShell 获取列表项
同样,您可以使用 -Query 参数通过 PowerShell 筛选和获取 SharePoint Online 中的列表项:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Projects"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#CAML Query to Filter List Items
$Query = "<View><Query><Where><Eq><FieldRef Name='ProjectStatus' /><Value Type='Choice'>Active</Value></Eq></Where></Query></View>"
#get all list items using powershell sharepoint online - matching given query
$ListItems = Get-PnPListItem -List $ListName -Query $Query
#Loop through each Item
foreach($ListItem in $ListItems)
{
Write-Host "Id :" $ListItem["ID"]
Write-Host "Title :" $ListItem["Title"]
}
使用脚本块从 SharePoint Online 检索列表项
以下是如何使用脚本块和进度栏从 SharePoint Online 列表中检索项目。
$List= Get-PnPList $ListName
$global:counter = 0;
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000 -Fields "Title" -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity "Getting List Items of '$($List.Title)'" -Status "Processing Items $global:Counter to $($List.ItemCount)";}
Write-host $ListItems.Count
同样,您可以通过指定 ViewFields 元素来获取具有特定字段的列表项。
#CAML Query to Get List Items with Specific fields
$Query = "<View><ViewFields><FieldRef Name='Title'/></ViewFields></View>"
#sharepoint online pnp powershell get list items
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000 -Query $Query
经常问的问题:
如何从 SharePoint 列表中获取超过 5000 个项目?
假设您有一个包含 > 5000 个项目的大型列表。在这种情况下,您可能必须对列表项进行批处理以克服阈值限制:“尝试的操作被禁止,因为它超出了管理员强制执行的列表视图阈值。” PowerShell 从 SharePoint Online 中的大型列表(>5000 个项目)中获取所有列表项目
您还可以使用带有 -PageSize 参数的 Get-PnPListItem。
如何从 PowerShell 导出 SharePoint 列表?
若要在 PowerShell 中将 SharePoint Online 列表导出到 CSV 文件,请使用以下 PowerShell 脚本:SharePoint Online PowerShell 将列表项导出到 CSV
如何使用 PowerShell 获取 SharePoint 列表中的项目数?
使用“List.ItemCount”属性可检索 SharePoint Online 列表或文档库中的项目总数。以下是用于获取 SharePoint Online 列表的项目计数的示例 PowerShell 脚本:用于获取列表项目计数的 SharePoint Online PowerShell
如何使用 PowerShell 从 SharePoint 文档库中获取文件列表?
要从 SharePoint Online 文档库检索所有文件,请使用 PnP PowerShell cmdlet:“Get-PnPListItem -List “库名称”。以下是示例脚本:SharePoint Online:使用 PowerShell 获取文档库中的文件列表
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag