SharePoint Online: PowerShell kullanarak Liste Öğesi Sayısını Alma


Gereksinim: PowerShell'i kullanarak SharePoint Online'da liste öğesi sayısını alın.

SharePoint Online'da Liste Öğesi Sayımı Nasıl Alınır?

Belirli bir SharePoint Online listesindeki öğelerin sayısını almanız gerekiyorsa bunu yapmanın birkaç yolu vardır. Bu yazıda, herhangi bir SharePoint Online listesinin öğe sayısını almak için PowerShell-CSOM'u nasıl kullanacağınızı göstereceğiz. Ayrıca liste öğelerinin sayısını almak için PnP PowerShell'i nasıl kullanacağınızı da göstereceğiz. Bu bilgilerle bir rapor oluşturmanız gerekiyorsa bu yararlı olabilir. Başlayalım!

SharePoint Online'daki "Site İçeriği" sayfası, ad, tür ve son değiştirilme tarihi bilgileriyle birlikte her liste ve kitaplıktaki öğe sayısını verir. (URL: https://.sharepoint.com/_layouts/Viewlsts.aspx)

SharePoint Online: Liste Öğesi Sayısını Alacak PowerShell

SharePoint Online'da PowerShell'i kullanarak bir liste öğesi sayısı mı almak istiyorsunuz? SharePoint Online listesindeki öğe sayısını bulmak için bu CSOM PowerShell betiğini kullanın:


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

Yukarıdaki betiği yeniden kullanılabilir bir fonksiyona saralım ve bir Liste veya belge kitaplığı için liste öğesi sayısını alalım.


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

Bu PowerShell betiği, SharePoint Online'daki liste öğesi sayısını alır

PnP PowerShell kullanarak SharePoint Online'da Liste Öğesi Sayısını Alma

Bir SharePoint Online sitesindeki tüm listelerden bir dizi öğe alalım. Raporlama veya başka amaçlar için bir kitaplıktaki dosya sayısına ilişkin doğru bir sayım almanız gerekiyorsa bu yararlı olabilir.


#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

Bu PowerShell betiğini, SharePoint Online'daki belge kitaplığındaki öğeleri saymak için de kullanabilirsiniz. Benzer şekilde, liste öğelerini belirli bir koşula göre filtreleyebilir ve sayısını alabilirsiniz:


#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

Site Koleksiyonundaki Tüm Listeler İçin Liste Öğesi Sayısı Nasıl Dışa Aktarılır?

PnP PowerShell'i kullanarak bir site koleksiyonundaki tüm listelerden ve kitaplıklardan öğe sayısını CSV biçimine nasıl aktaracağınız aşağıda açıklanmıştır.


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

Sonuç :

Bir belge kitaplığındaki dosyaları saymak isterseniz ne olur?

SharePoint Online: PowerShell kullanarak Belge Kitaplığındaki Öğeleri Sayma

SharePoint Online listesinin ItemCount özelliği size dosya ve klasörlerin birlikte dahil olduğu öğelerin sayısını verdiğinden, Kitaplıktaki dosya ve klasörleri nasıl sayarız? İşte dosya ve klasörlerin sayısını almak için PowerShell:


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

Ayrıca liste öğeleri arasında geçiş yapabilir ve Dosya ve Klasörlerin sayısını almak için $ListItem.FileSystemObjectType -eq “File” veya “Folder” öğesini kontrol edebilirsiniz. İşte SharePoint Online Sitesinde büyük listeler almak için diğer paylaşımım SharePoint Online: PowerShell Kullanarak Tüm Büyük Listeleri Nasıl Bulunur?

İlgili makaleler