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

[玩转系统] SharePoint Online:使用 PowerShell 获取列表项计数

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

SharePoint Online:使用 PowerShell 获取列表项计数


要求:使用 PowerShell 获取 SharePoint Online 中的列表项计数。

如何获取 SharePoint Online 中的列表项计数?

如果您需要获取给定 SharePoint Online 列表中的项目计数,有几种方法可以实现。在本文中,我们将向您展示如何使用 PowerShell-CSOM 获取任何 SharePoint Online 列表的项目计数。我们还将向您展示如何使用 PnP PowerShell 获取列表项计数。如果您必须生成包含此信息的报告,这会很有用。让我们开始吧!

SharePoint Online 中的“网站内容”页面为您提供每个列表和库中的项目数量,以及名称、类型和上次修改日期信息。 (网址:https://.sharepoint.com/_layouts/Viewlsts.aspx

[玩转系统] SharePoint Online:使用 PowerShell 获取列表项计数

SharePoint Online:用于获取列表项计数的 PowerShell

您是否希望在 SharePoint Online 中使用 PowerShell 获取列表项计数?使用此 CSOM 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/sites/marketing"
$ListName = "Documents"

#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 List
    $List= $Ctx.web.lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
 
    #Get List Item Count
    Write-host -f Green "Total Number of Items in the List:"$List.ItemCount
}
Catch {
    write-host -f Red "Error Getting List Item Count!" $_.Exception.Message
}

让我们将上面的脚本包装成一个可重用的函数,并获取列表或文档库的列表项计数。


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

Function Get-SPOListItemCount{
    <#
        .SYNOPSYS
           Returns the Item Count for the specified list or library
        .DESCRIPTION
           Returns the Item Count for the specified list or library
        .PARAMETER WebUrl
            The URL of the site that contains the list or library
        .PARAMETER ListName
            The title of the list or library to get count
        .EXAMPLE
           Get-SPOListItemCount -WebUrl "https://tenant.sharepoint.com/teams/marketing" -ListName "Shared Documents"
    #>
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        [Parameter(Mandatory=$true, HelpMessage="The URL of the site that contains the list or library", Position=0)] [string]$WebUrl,
        [Parameter(Mandatory=$true, HelpMessage="The Title of the list or library", Position=1)] [string]$ListName
    )

    Begin{
        #Get Credentials to connect
        $Credential = Get-Credential
        #Setup context to connect
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credential.UserName, $Credential.Password)
    }
    Process{
        try{
            #Get the Web
            $Web = $Ctx.Web
            #Get the List
            $List = $web.Lists.GetByTitle($ListName)
            $Ctx.Load($List)
            $Ctx.ExecuteQuery()
            #sharepoint online count items in list
            Return $List.ItemCount
        }
        catch{
            Write-Host -ForegroundColor Red $_.Exception.Message
            return 0
        }

    }
    End{
        $Ctx.Dispose()
    }
}
    
#Call the function to get item count
Get-SPOListItemCount -WebUrl "https://crescent.sharepoint.com/sites/marketing" -ListName "Documents"

此 PowerShell 脚本获取 SharePoint Online 中的列表项计数

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

让我们从 SharePoint Online 网站的所有列表中获取一些项目。如果您需要准确计算库中的文件数量以用于报告或其他目的,这会很有用。


#Variable
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"

#Connect to PnP Online
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)

#Get List Item count from all Lists from the Web
Get-PnPList | Select Title, ItemCount

您还可以使用此 PowerShell 脚本对 SharePoint Online 文档库中的项目进行计数。同样,您可以根据特定条件过滤列表项并获取其计数:


#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Tasks"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get list items - Filtered by given condition
$ListItems = Get-PnPListItem -List $ListName -Query "<View><Query><Where><Eq><FieldRef Name='Status'/><Value Type='Text'>Completed</Value></Eq></Where></Query></View>"

#Get List Items Count
$ListItems.Count

如何导出网站集中所有列表的列表项计数?

以下介绍了如何使用 PnP PowerShell 将网站集中的所有列表和库中的项目计数导出为 CSV 格式。


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$CSVFile = "C:\temp\ListItemCount.csv"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Get All Webs from the site collection
    $SubWebs = Get-PnPSubWeb -Recurse -IncludeRootWeb
    $ListInventory= @()
    Foreach ($Web in $SubWebs)
    {
        Write-host -f Yellow "Getting List Item count from site:" $Web.URL
        #Connect to Subweb
        Connect-PnPOnline -Url $Web.URL -Interactive

        #Get all lists and libraries of the Web
        $ExcludedLists  = @("Reusable Content","Content and Structure Reports","Form Templates","Images","Pages","Workflow History","Workflow Tasks", "Preservation Hold Library")
        $Lists= Get-PnPList | Where {$_.Hidden -eq $False -and $ExcludedLists -notcontains $_.Title}
        foreach ($List in $Lists)
        {
            $Data = new-object PSObject
            $Data | Add-member NoteProperty -Name "Site Name" -Value $Web.Title
            $Data | Add-member NoteProperty -Name "Site URL" -Value $Web.Url
            $Data | Add-member NoteProperty -Name "List Title" -Value $List.Title
            $Data | Add-member NoteProperty -Name "List URL" -Value $List.RootFolder.ServerRelativeUrl
            $Data | Add-member NoteProperty -Name "List Item Count" -Value $List.ItemCount
            $ListInventory += $Data
        }
    }
    $ListInventory | Export-CSV $CSVFile -NoTypeInformation
    Write-host -f Green "List Inventory Exported to Excel Successfully!"
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

结果 :

[玩转系统] SharePoint Online:使用 PowerShell 获取列表项计数

如果您想统计文档库中的文件数量怎么办?

SharePoint Online:使用 PowerShell 对文档库中的项目进行计数

由于 SharePoint Online 列表的 ItemCount 属性为您提供项目计数,包括文件和文件夹,因此我们如何对库中的文件和文件夹进行计数?好吧,这是获取文件和文件夹数量的 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 Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"

#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 List
    $List= $Ctx.web.lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
 
    #Define Query to Filter and Get All Files from the list
    $Query = "@
    <View Scope='RecursiveAll'>  
            <Query> 
               <Where>
                     <Eq>
                           <FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>
                     </Eq>
               </Where> 
            </Query> 
    </View>"
    $FilesQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
    $FilesQuery.ViewXml =$Query
    $Files = $List.GetItems($FilesQuery)
    $Ctx.Load($Files)
    $Ctx.ExecuteQuery() 

    #Define Query to Filter and Get All Folders from the list
    $Query = "@
    <View Scope='RecursiveAll'>  
            <Query> 
               <Where>
                     <Eq>
                           <FieldRef Name='FSObjType' /><Value Type='Integer'>1</Value>
                     </Eq>
               </Where> 
            </Query> 
    </View>"
    $FoldersQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
    $FoldersQuery.ViewXml =$Query
    $Folders = $List.GetItems($FoldersQuery)
    $Ctx.Load($Folders)
    $Ctx.ExecuteQuery() 

    #Get List Item Count
    Write-host -f Green "Total Number of Items in the List:"$List.ItemCount
    Write-host -f Green "Total Number of Files in the List:"$Files.Count
    Write-host -f Green "Total Number of Folders in the List:"$Folders.Count
}
Catch {
    write-host -f Red "Error Getting List Item Count!" $_.Exception.Message
}

您还可以循环遍历列表项并检查 $ListItem.FileSystemObjectType -eq “File” 或 “Folder” 以获取文件和文件夹计数。这是我在 SharePoint Online 网站 SharePoint Online 中获取大型列表的另一篇文章:如何使用 PowerShell 查找所有大型列表?

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

取消回复欢迎 发表评论:

关灯