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

[玩转系统] 如何使用 PnP PowerShell 获取 SharePoint 列表中的项目计数?

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

如何使用 PnP PowerShell 获取 SharePoint 列表中的项目计数?


最近,我需要显示 SharePoint Online 列表项计数。我为此使用了 PnP PowerShell。在本教程中,我将向您展示如何使用 PnP PowerShell 获取 SharePoint 列表中的项目计数

使用 PnP PowerShell 获取 SharePoint 列表中的项目计数

让我向您展示使用 PnP PowerShell 获取 SharePoint 列表中的项目计数的不同方法。

在这里,我有一个 SharePoint Online 列表,我在其中添加了一些项目。下面的屏幕截图显示了 SharePoint 列表的外观。

[玩转系统] 如何使用 PnP PowerShell 获取 SharePoint 列表中的项目计数?

我们将在此处了解如何使用 PnP PowerShell 获取 SharePoint 列表项计数。

1. 使用 Get-PnPList 获取列表项计数

在 PnP PowerShell 中,您可以使用列表对象的 ItemCount 属性来获取 SharePoint Online 中的项目计数。

这里有一个完整的PnP PowerShell脚本,你可以看看。

# Connect to SharePoint Online
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerPlatformFAQs" -Interactive

# Get the list
$list = Get-PnPList -Identity "LargeInventoryList"

# Get the item count
$itemCount = $list.ItemCount

Write-Output "Total items in the list: $itemCount"

使用 VS 代码执行上述 PowerShell 脚本后,您可以看到 SharePoint Online 列表项计数,如下面的屏幕截图所示。我在本地系统中执行了上述脚本。

[玩转系统] 如何使用 PnP PowerShell 获取 SharePoint 列表中的项目计数?

阅读如何使用 PnP PowerShell 检查 SharePoint Online 中是否存在文件夹

2. 使用 Get-PnPListItem 获取列表项计数

PnP PowerShell Get-PnPListItem cmdlet 从 SharePoint 列表中检索项目。要获取项目数,您可以使用 -PageSize 参数来有效处理大型列表。

这是一个完整的 PnP PowerShell 脚本。

# Connect to SharePoint Online
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerPlatformFAQs" -Interactive

# Get the list
$list = Get-PnPList -Identity "LargeInventoryList"

# Get all items in the list
$items = Get-PnPListItem -List $list -PageSize 5000 -Fields "ID"

# Get the count of items
$itemCount = $items.Count

Write-Output "Total items in the list: $itemCount"

如果列表很大,此方法特别有用。我使用 VS code 执行了上述 PowerShell 脚本,您可以在下面的屏幕截图中看到输出。它为我提供了准确的项目计数列表。

[玩转系统] 如何使用 PnP PowerShell 获取 SharePoint 列表中的项目计数?

阅读使用 PnP PowerShell 将文件上传到 SharePoint 文档库

3. 从特定文件夹获取项目计数

如果您需要获取 SharePoint 列表或库中特定文件夹中的项目计数,可以使用 Get-PnPFolderItem PnP PowerShell cmdlet。

这是一个完整的 PowerShell 脚本。

# Connect to SharePoint Online
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerPlatformFAQs" -Interactive

# Define the library and folder
$libraryName = "MyLibrary"
$folderPath = "/sites/PowerPlatformFAQs/MyLibrary/MyFolder"

# Get the items in the folder
$folderItems = Get-PnPListItem -List $libraryName -FolderServerRelativeUrl $folderPath -PageSize 5000

# Get the count of items in the folder
$itemCount = $folderItems.Count

Write-Output "Total items in the folder: $itemCount"

Get-PnPListItem cmdlet 与 -FolderServerRelativeUrl 参数一起使用来指定文件夹路径。这将检索指定文件夹中的所有项目。

我执行了上面的 PnP PowerShell 脚本,您可以在下面的屏幕截图中看到它给出了 SharePoint Online 列表或库文件夹中的确切项目计数。

[玩转系统] 如何使用 PnP PowerShell 获取 SharePoint 列表中的项目计数?

结论

在本教程中,我解释了如何使用 PnP PowerShell 获取 SharePoint 列表中的项目计数。我还解释了如何使用 PnP PowerShell 对大型 SharePoint Online 列表中的项目进行计数。我们还了解了如何使用 PnP PowerShell 对 SharePoint 列表或文档库中的文件夹中的项目进行计数。

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

取消回复欢迎 发表评论:

关灯