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

[玩转系统] SharePoint Online:PowerShell 获取所有文档库

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

SharePoint Online:PowerShell 获取所有文档库


要求: PowerShell 列出 SharePoint Online 中的所有文档库。

如何获取SharePoint Online站点中的所有文档库?

如果您正在管理 SharePoint Online 网站并希望获取 SharePoint Online 网站中的所有文档库,可以使用两种方法!这篇博文将向您展示如何获取 SharePoint Online 中的所有文档库。

SharePoint 中的“网站内容”页面显示网站的所有列表和库,以及每个列表或库中的项目数。

[玩转系统] SharePoint Online:PowerShell 获取所有文档库

让我们看看 SharePoint Online PowerShell 列出子网站(或网站!)中的文档库。

SharePoint Online PowerShell 列出文档库

让我分享一些您需要的方便脚本,以快速列出用于报告或其他目的的所有文档库。以下是用于从 SharePoint Online 网站获取所有文档库的 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"
  
#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
   
#Get Lists from the web
$Ctx.Load($Ctx.Web.Lists)
$Ctx.executeQuery()
 
#Filter Document Libraries from Lists collection
$Lists = $Ctx.Web.Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False}

#Loop through each document library and Get the Title
Foreach ($List in $Lists)
{
    Write-host $List.Title
}

这将使用 PowerShell 获取 SharePoint Online 中的所有文档库。

PnP PowerShell 获取所有文档库及其 ID

如何使用 PowerShell 获取 SharePoint Online 中的文档库列表?下面是使用 Get-PnPList cmdlet 的 PnP PowerShell 方式:


#Variables
$Site = "https://crescent.sharepoint.com/sites/retail"

#Connect with SharePoint Online
Connect-PnPOnline -Url $Site -Interactive 

#Get all document libraries
$DocLibs = Get-PnPList | Where-Object {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false }

#Get ID and Title of the document library
$DocLibs | Select ID, Title

SharePoint Online 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"
   
#Function to Get all documents Libraries in a SharePoint Online Site Collection
Function Get-SPODocumentLibrary($SiteURL)
{
    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
   
        #Get the web and Its subsites from given URL
        $Web = $Ctx.web
        $Ctx.Load($Web)
        $Ctx.Load($Web.Lists)
        $Ctx.Load($web.Webs)
        $Ctx.executeQuery()
 
        Write-host -f Yellow "Processing Site: $SiteURL"
 
        #sharepoint online powershell list all document libraries
        $Lists = $Web.Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False}

        #Loop through each document library and Get the Title
        Foreach ($List in $Lists)
        {
            Write-host $List.Title
        }
  
        #Iterate through each subsite of the current web and call the function recursively
        ForEach ($Subweb in $Web.Webs)
        {
            #Call the function recursively to process all subsites
            Get-SPODocumentLibrary($Subweb.url)
        }
    }
    Catch {
        write-host -f Red "Error Getting Document Libraries!" $_.Exception.Message
    }
}
  
#Config Parameters
$SiteCollURL="https://crescent.sharepoint.com"
  
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
#Call the function to get all document libraries in a site collection
Get-SPODocumentLibrary $SiteCollURL

要使用 PnP PowerShell 列出 SharePoint Online 中的所有文档库,请使用:使用 PnP PowerShell 获取 SharePoint Online 中的所有文档库

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

取消回复欢迎 发表评论:

关灯