SharePoint Online: obtenga todas las carpetas de la lista o biblioteca usando PowerShell
Requisito: Obtener todas las carpetas de una lista de SharePoint Online mediante PowerShell.
PowerShell para obtener todas las carpetas de una lista de SharePoint Online
En muchos casos, es posible que necesite obtener una lista de todas las carpetas dentro de una lista o biblioteca de SharePoint Online. Esto se puede lograr usando PowerShell. ¡Esta publicación de blog le mostrará cómo obtener todas las carpetas de una lista o biblioteca usando PowerShell!
Este es el enfoque basado en CAML para obtener todas las carpetas y subcarpetas de forma recursiva de una lista:
#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"
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
#Get 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)
#Define CAML Query to get all folders from list recursively
$CAMLQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$CAMLQuery.ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType'/><Value Type='Integer'>1</Value></Eq></Where></Query></View>"
#Get All Folders from the List
$Folders = $List.GetItems($CAMLQuery)
$Ctx.Load($Folders)
$Ctx.ExecuteQuery()
#Iterate through Each Folder
ForEach($Folder in $Folders)
{
#Get the Folder's Server Relative URL
Write-host $Folder.FieldValues['FileRef']
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Este PowerShell obtiene la lista de carpetas de una biblioteca de SharePoint Online.
SharePoint Online: obtenga subcarpetas de una lista de SharePoint Online usando PowerShell
Obtengamos todas las carpetas de nivel superior de una lista o biblioteca en SharePoint Online.
#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"
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
#Get 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)
#Get Sub-Folders of the List
$SubFolders = $List.RootFolder.Folders
$Ctx.Load($SubFolders)
$Ctx.ExecuteQuery()
#Iterate through Each SubFolder
ForEach($Folder in $SubFolders)
{
#Get the Folder's Server Relative URL
Write-host $Folder.Name
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
De manera similar, si desea obtener todas las subcarpetas de una carpeta específica, use:
#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"
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$FolderRelativeURL = "/sites/marketing/Shared Documents/2018"
#Get 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 Sub-Folders of a Folder
$SubFolders = $Ctx.web.GetFolderByServerRelativeUrl($FolderRelativeURL).Folders
$Ctx.Load($SubFolders)
$Ctx.ExecuteQuery()
#Iterate through Each SubFolder
ForEach($Folder in $SubFolders)
{
#Get the Folder's Server Relative URL
Write-host $Folder.Name
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
SharePoint Online: obtenga todas las carpetas de forma recursiva desde una lista de SharePoint Online mediante PowerShell
Aquí está el script de PowerShell para obtener de forma recursiva todas las carpetas de una lista o biblioteca de SharePoint Online.
#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 Sub-folders of a Folder in SharePoint Online
Function Get-SPOFolders([Microsoft.SharePoint.Client.Folder]$Folder)
{
Try {
Write-host $Folder.ServerRelativeUrl
#Process all Sub Folders
$Ctx.Load($Folder.Folders)
$Ctx.ExecuteQuery()
#Iterate through each sub-folder of the folder
Foreach ($Folder in $Folder.Folders)
{
#Call the function recursively
Get-SPOFolders $Folder
}
}
Catch {
write-host -f Red "Error Getting Folder!" $_.Exception.Message
}
}
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
#Get 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 Root Folder of a List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$RootFolder = $List.RootFolder
$Ctx.Load($RootFolder)
$Ctx.ExecuteQuery()
#Call the Function to get all folders recursively from the list
Get-SPOFolders $RootFolder
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
PnP PowerShell para obtener todas las carpetas de una biblioteca de documentos de SharePoint Online
A continuación se explica cómo obtener todas las carpetas (y subcarpetas) de forma recursiva desde una lista o biblioteca de SharePoint Online:
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Branding"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get All Folders from the document Library
$Folders = Get-PnPFolder -List $ListName
Write-host "Total Number of Items in the Folder in the list:" $Folders.Count
#Get Folder/Subfolder details
$Folders | Select Name, TimeCreated, ItemCount, ServerRelativeUrl
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Obtengamos todas las carpetas de una biblioteca de documentos determinada y exportémoslas a un archivo CSV usando PnP PowerShell.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/legal"
$ListName = "Work"
$CSVPath = "C:\Temp\Folders.csv"
$FoldersInventory = @()
#Connect to Site
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the List
$List = Get-PnPList -Identity $ListName
#Get all Folders from List - with progress bar
$global:counter = 0;
$Folders = Get-PnPListItem -List $List -PageSize 500 -Fields FileLeafRef -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity "Getting Folders from List:" -Status "Processing Items $global:Counter to $($List.ItemCount)";} | Where {$_.FileSystemObjectType -eq "Folder"}
#Iterate through all folders in the list
$Folders | ForEach-Object {
#Collect Folder data
$FoldersInventory += [PSCustomObject] @{
FolderName = $_.FieldValues.FileLeafRef
URL = $_.FieldValues.FileRef
}
}
$FoldersInventory
$FoldersInventory | Export-Csv -Path $CSVPath -NoTypeInformation
