Accéder au contenu principal

SharePoint Online: Script PowerShell pour supprimer une colonne dans tous les sites d’une collection

 Dans la gestion de collection de sites SharePoint avec un grand nombre de sous-sites, il peut être utile de pouvoir nettoyer des colonnes ajoutées par mégarde.

Ainsi le script suivant permet de supprimer une colonne spécifiée de tous les type de contentu des librairies “Pages” de tous les sous-sites de la collection.

Libre à vous de l’adapter à votre convenance.

[string]$username = "Admin@yourtenant.onmicrosoft.com"
[string]$PwdTXTPath = "C:\SECUREDPWD\ExportedPWD-$($username).txt"
$secureStringPwd = ConvertTo-SecureString -string (Get-Content $PwdTXTPath)
$adminCreds = New-Object System.Management.Automation.PSCredential $username, $secureStringPwd

[string]$RootSiteToCheck = "https://yourtenant.sharepoint.com/sites/YourSiteCollection"
[string]$SPListToCheck = "Pages"
[string]$SPFieldInternalNameToCheck = "PublishedDate"

function Load-DLLandAssemblies
{
    [string]$defaultDLLPath = ""
    # Load assemblies to PowerShell session
    $defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
    [System.Reflection.Assembly]::LoadFile($defaultDLLPath)
    $defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
    [System.Reflection.Assembly]::LoadFile($defaultDLLPath)
    $defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
    [System.Reflection.Assembly]::LoadFile($defaultDLLPath)
}

function Check-And-Fix-Field-In-SPList ([Microsoft.SharePoint.Client.ClientContext]$Context, [Microsoft.SharePoint.Client.Web]$CurrentWeb)
{
    $MyCurrentListTocheck = $CurrentWeb.Lists.GetByTitle($SPListToCheck)
    $AllSPListCT = $MyCurrentListTocheck.ContentTypes
    $Context.Load($MyCurrentListTocheck)
    $Context.Load($AllSPListCT)
    $Context.ExecuteQuery()
    Write-Host "  ===>>> SubSite to check:", $CurrentWeb.Title, "- URL:", $CurrentWeb.Url -ForegroundColor Green
    Write-Host "  ===>>> List to check:", $MyCurrentListTocheck.Title -ForegroundColor Green
    foreach($MySpListCT in $AllSPListCT)
    {
        Write-Host "         -->> Content Type Name:", $MySpListCT.Name
        Write-Host "         -->> Content Type ID:", $MySpListCT.id
        $Myfields = $MySpListCT.Fields
        $Context.Load($Myfields)
        $Context.ExecuteQuery()
        $MyfieldToCheck = ($Myfields | where {$_.InternalName -eq $SPFieldInternalNameToCheck})
        if($MyfieldToCheck -ne $null)
        {
            Write-Host "             ---------------------------------------------- " -ForegroundColor Yellow
            Write-Host "             >>>> Field Name:", $MyfieldToCheck.Title -ForegroundColor Yellow
             Write-Host "             >>>> Field InternalName:", $MyfieldToCheck.InternalName, "- Field ID:", $MyfieldToCheck.id -ForegroundColor Yellow
            Write-Host "             >>>> Field Required:", $MyfieldToCheck.Required -ForegroundColor Yellow
            Write-Host "             ---------------------------------------------- " -ForegroundColor Yellow
            $MyfieldToCheck.DeleteObject();
            $MySpListCT.Update($false);
            $Context.Load($MySpListCT);
            $Context.ExecuteQuery()
            Write-Host "             >>>> Field Deleted !!!!" -ForegroundColor Red
        }
    }
}

function Get-SPOSubWebs
{
    Param(
        [Microsoft.SharePoint.Client.ClientContext]$Context,
        [Microsoft.SharePoint.Client.Web]$RootWeb
    )
    $Webs = $RootWeb.Webs
    $Context.Load($Webs)
    $Context.ExecuteQuery()
    ForEach ($sWeb in $Webs)
    {
        Write-host "   ====>> SubSite:", $sWeb.URL -ForegroundColor red
        Check-And-Fix-Field-In-SPList $Context $sWeb
        Get-SPOSubWebs -RootWeb $sWeb -Context $Context
    }
}

cls
Write-Host " ---------------------------------------------- "
Load-DLLandAssemblies
Write-Host " ---------------------------------------------- "
Write-host "==================================================================” -ForegroundColor Green
$mySitectx = New-Object Microsoft.SharePoint.Client.ClientContext($RootSiteToCheck)
$mySitectx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($adminCreds.UserName, $adminCreds.Password)
$mySitectx.RequestTimeout = 1000000 # milliseconds
$myCurrentWeb = $mySitectx.Web
$mySitectx.Load($myCurrentWeb)
$mySitectx.ExecuteQuery()
Write-Host " "
Write-Host " ---------------------------------------------------------"
Write-Host "  >>>> # Server Version:" $mySitectx.ServerVersion " # <<<<<<" -ForegroundColor Green
Write-Host " ---------------------------------------------------------"
Write-Host " "
Write-host "=================================================================="
Write-host "   -->> RootSite:", $myCurrentWeb.URL -ForegroundColor green
Write-host "=================================================================="
Check-And-Fix-Field-In-SPList $mySitectx $myCurrentWeb
Get-SPOSubWebs $mySitectx $myCurrentWeb
Write-host "=================================================================="

Fabrice Romelard

Version anglaise:

Sources utilisées:

Commentaires

Posts les plus consultés de ce blog

Série de Videos sur Home Assistant intégrant la production Photovoltaïque

 Un certain nombre de vidéos sont en ligne pour intégrer sa production photovoltaïque dans Home Assistant en partant de la base. Installation de Home Assistant: On peut ensuite intégrer les composant des Micro-Onduleurs Enphase, mais aussi les batteries Enphase: Ou encore le composant de contrôle Ecojoko: Ce qui permet alors de faire des comparaisons entre les valeurs capturées: Des videos seront encore publiés dans les prochaines semaines sur différents aspects de cette solution. Fab

Nouveau Scripts PowerShell publiés pour gérer les Storage Accounts dans Azure Cloud

Deux scripts sont en lignes pour nettoyer des Storage Account dans Azure Cloud: Supprimer les Blobs d'un container existant https://techcommunity.microsoft.com/discussions/azure/powershell-script-to-remove-all-blobs-from-storage-account/4357815 ------------------------------------------------------- [string]$myConnectionString = "DefaultEndpointsProtocol=https;AccountName=YourStorageAccountName;AccountKey=YourKeyFromStorageAccountConnectionString;EndpointSuffix=core.windows.net" [string]$ContainerName = "YourBlobContainerName" [int]$blobCountAfter = 0 [int]$blobCountBefore = 0 $context = New-AzStorageContext -ConnectionString $myConnectionString $blobCountBefore = (Get-AzStorageBlob -Container $ContainerName -Context $context).Count Write-Host "Total number of blobs in the container Before deletion: $blobCount" -ForegroundColor Yellow Get-AzStorageBlob -Container $ContainerName -Context $context | ForEach-Object {     $_ | Remove-AzureStorageBlob   # o...

Série de vidéos sur le montage d'une serre horticole ACD

 Episode 1: Préparation du terrain Episode 2: Montage de la serre en elle même Episode 3: Finalisation avec le montage électrique alimentant la serre Bon visionnage Fab