Búsqueda de sitios web

PowerShell para crear una carpeta en la biblioteca de documentos de SharePoint


Requisito: Crear una nueva carpeta en la biblioteca de documentos de SharePoint Server.

¿Cómo agregar una carpeta en la biblioteca de documentos de SharePoint usando PowerShell?

Al usar PowerShell, puede crear rápidamente carpetas en SharePoint. Esta publicación de blog aprenderá cómo usar PowerShell para crear una nueva carpeta en una biblioteca de documentos de SharePoint.

Para crear una carpeta en la biblioteca de documentos en SharePoint Server, use este script de PowerShell:


$folder = $list.Folders.Add("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "New")
$folder.Update()

El script anterior no proporciona mucha tolerancia a fallos, ¿no es así?

  • ¿Qué pasa si la biblioteca particular que estamos buscando no existe?
  • ¿Qué pasa si la carpeta que intentamos agregar ya existe en la biblioteca?

¡Error! ¡Así que reescribamos el código en PowerShell con poca tolerancia a fallas!

¿Cómo crear carpetas y subcarpetas en la lista de SharePoint usando PowerShell?

Aquí está el fragmento de código de PowerShell para crear carpetas y subcarpetas dentro de la lista o biblioteca de SharePoint:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Get the Web
$web=Get-SPWeb "https://sharepoint.company.com"
#Get the List/Library
$list=$web.Lists.TryGetList("SalesList")

If($list)  #Check If List exists!
{
    #Check Folder Doesn't exists in the Library!
    $folder = $list.ParentWeb.GetFolder($list.RootFolder.Url + "/" +"Sales List");
    #sharepoint powershell check if folder exists
    If($folder.Exists -eq $false)
    {
       #Create a Folder
       $folder = $list.AddItem([string]::Empty, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "Sales Sub-Item")
       $folder.Update();
    }
   
    #Create a Sub-Folder "APAC Sales Documents" inside "Sales Documents"    
    $Subfolder = $list.ParentWeb.GetFolder($folder.URL + "/" + "APAC Sales Documents");
    
    #Check if sub-folder doesn't exist already
    If ($Subfolder.Exists -eq $false)
    {
       #Create a Sub-Folder Inside "Sales Documents"
       $Subfolder = $list.AddItem($folder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "APAC Sales Documents")
       $Subfolder.Update();
    }
}

¿Qué tal si garantizamos una estructura de carpetas anidadas en la biblioteca de SharePoint? P.ej. Biblioteca/CarpetaA/CarpetaAA/CarpetaAAA


#Get the Web
$web=Get-SPWeb "https://sharepoint.company.com"

#Get the List/Library
$list=$web.Lists.TryGetList("Documents")

#Function to Ensure FolderPath
Function Ensure-FolderPath($FolderPath)
{
    $SubFolders = $FolderPath -split "/"
    $RootFolder =  $SubFolders[0]
    $SubfolderPath = $RootFolder

    For($i =1; $i -le $SubFolders.count-1; $i++)
    {        
        $SubfolderPath = $SubfolderPath+"/"+$SubFolders[$i]
        $Folder = $Web.GetFolder($SubfolderPath)

        #Create Folder if it doesn't exists
        If($Folder.Exists -eq $false)
        {
            $TargetFolder = $Web.Folders.Add($SubfolderPath)
            Write-host "Folder Created:"$SubfolderPath
        }
    }
}

#Call the function to Ensure Folder Path
Ensure-FolderPath "Documents/Technical Design/Assorted/2020"

Artículos relacionados