SharePoint solution: How to get the number of items in the Library or List

51CTO blog address: https://blog.51cto.com/13969817blog
garden blog address: https://www.cnblogs.com/bxapollo

Today I will share with you how to use the PowerShell CSOM script to obtain the Item count for a specific Library or List, including the items in all the folders under it. This is conducive to data analysis and statistics. For example, organizational structure changes require data migration. , Then you can use this method to compare the data volume before migration and the destination item count after migration to ensure that the data before and after migration are consistent.

There are many options for obtaining Item count, such as PnP PowerShell, etc. This article will introduce you to the PowerShell CSOM script solution.

The specific implementation is divided into the following 3 steps:

  • Load SharePoint CSOM Assemblies
  • Process variables and ensure connection to SharePoint Online
  • The custom function gets the number of items from the list of a specific website URL

Command to 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"

But because the .net of my environment is 4.0, by default, the function of executing code in assemblies loaded from remote locations is disabled, so you need to use [System.Reflection.Assembly]::LoadFrom() to load Microsoft.SharePoint .Client.dll" as shown below:

SharePoint solution: How to get the number of items in the Library or List

Note: To load these two dll files, you need to execute it on the SharePoint Server deployment side, otherwise the physical path does not have the file by default.

Process variables and ensure connection to SharePoint Online

$SiteUrl = "https://mvptrainingcn.sharepoint.com/sites/Demo2"
$ListName="TrainingDocument"
SharePoint solution: How to get the number of items in the Library or List

Note: You need to enter the account and password of Microsoft 365 Global Admin, as shown below: br/>$UserName="[email protected]"
$Password ="XXXXXX"
SharePoint solution: How to get the number of items in the Library or List

The custom function gets the number of items from the list of a specific website URL

#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

SharePoint solution: How to get the number of items in the Library or List

You can see that the number of items obtained is 2, which is consistent with the actual situation of SharePoint Online, as shown in the following figure:

SharePoint solution: How to get the number of items in the Library or List

I hope that the statistical methods shared this time will be helpful to everyone. Keep paying attention to me. I will share more tips in the future. Thank you for reading.

Guess you like

Origin blog.51cto.com/13969817/2592078