SharePoint Online:使用 PowerShell 获取所有列表
要求: SharePoint Online 使用 PowerShell 获取所有列表库存。
SharePoint 列表是信息容器,类似于数据库中的表。它使您能够存储、组织信息并与组织中的其他人共享信息。如果您管理 SharePoint Online,您可能会发现自己处于需要获取网站上的列表或所有列表的情况。您可以使用 PowerShell 来执行此操作。本文将向您展示如何使用 PowerShell 获取 SharePoint Online 网站上的所有列表。
使用 PowerShell 获取 SharePoint Online 中的所有列表有两种方法。第一种方法是使用 CSOM 方法并获取 Web.Lists 集合。这将列出 SPOList 对象以及列表的各种属性,例如标题、URL 和项目计数。第二种方法是将 Get-PnPList cmdlet 与 PnP PowerShell 结合使用,这将检索 SharePoint Online Web 中的所有列表。
使用 PowerShell CSOM 获取 SharePoint Online 中的所有列表:
在这篇博文中,我们将了解如何使用 PowerShell 获取 SharePoint Online 网站中所有列表的列表。如果您想要生成所有列表的列表以用于审核或报告目的、解决列表问题或只是获取网站上所有列表的概述,这会很方便。让我们看看它是如何工作的!
以下是使用 CSOM 获取 SharePoint Online 网站中所有列表的 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"
#Function to Get all/specific list from site
Function Get-SPOList()
{
Param
(
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Web] $Web,
[Parameter(Mandatory=$false)] [string] $ListName
)
#Get the Context
$Ctx = $Web.Context
#Get a single list or All Lists
If($ListName)
{
#sharepoint online get list powershell
$List = $Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
Return $List
}
Else
{
#sharepoint online get all lists powershell
$Lists = $Web.Lists
$Ctx.Load($Lists)
$Ctx.ExecuteQuery()
Return $Lists
}
}
#Parameters
$SiteURL="https://Crescent.sharepoint.com"
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#sharepoint online powershell get all lists
$Lists = Get-SPOList -Web $Ctx.Web
#Extract List data
$ListCollection = @()
ForEach($List in $Lists)
{
$ListData = New-Object -TypeName PSObject
$ListData | Add-Member -MemberType NoteProperty -Name "Title" -Value $List.Title
$ListData | Add-Member -MemberType NoteProperty -Name "Itemcount" -Value $List.Itemcount
$ListData | Add-Member -MemberType NoteProperty -Name "BaseTemplate" -Value $List.BaseTemplate
$ListData | Add-Member -MemberType NoteProperty -Name "Created" -Value $List.Created
$ListData | Add-Member -MemberType NoteProperty -Name "LastItemModifiedDate" -Value $List.LastItemModifiedDate
$ListCollection += $ListData
}
#Export List Inventory to CSV
$ListCollection | Export-csv -Path "C:\Temp\list-inventory.csv" -NoTypeInformation
此 PowerShell 根据给定参数从 SharePoint Online 网站获取单个列表或所有列表。
如何使用 PowerShell 获取 SharePoint Online 中的列表?
要获取 SharePoint Online 中的列表,请使用:
#Call the function to get a List
$List = Get-SPOList -web $ctx.Web -ListName "Documents"
Write-host "Total Number of Items in List:" $List.ItemCount
PnP PowerShell 从 SharePoint Online 站点获取所有列表
您可以使用 Get-PnPList 获取站点中的所有列表,包括隐藏列表。以下示例获取网站 https://crescent.sharepoint.com/sites/Marketing 中的所有列表。
让我们从当前的 SharePoint Online 网站获取所有列表。
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get all lists
$Lists = Get-PnPList
#Get List Title, Description and Number of Items
$Lists | Select Title, Description, ItemCount
您还可以将 Identity 参数与列表名称、GUID 或列表 URL 结合使用,从站点获取特定列表。同样,要使用 PnP PowerShell 获取 SharePoint Online 中的特定列表,请使用:
#Connect to PnP Online
Connect-PnPOnline -Url "https://Crescent.sharepoint.com" -Credentials (Get-Credential)
#Get the List
$List = Get-PnPList -Identity "Documents"
#Get Number of Items in the List
Write-host "Total Number of List Items:"$List.ItemCount
