En cas de migration de contenu dans SharePoint Online, il peut être très utile de pouvoir nettoyer le résultat.
Le cas standard est une liste ayant un nombre d’élément important (voir allucinant: par exemple 200’000 items), qui est au dessus de toutes les limites imposées par Microsoft en dur:
- Vue limitée à 2’000
- Alerte de liste à 5’000
- Blocage de liste à 20’000
Ainsi la solutione st de faire 2 migrations de cette même liste (ou plus suivant le découpage souhaité):
- Premier chargement = Liste vivante ayant les XX derniers mois de contenu (par exemple l’année courante)
- Second chargement = liste d’archive ayant l’intégralité du contenu
- Chargements optionels = Si on souhaite faire un découpage par année
De ce fait tous les chargements qui devront être nettoyés après coup, demande la suppression d’un très grand nombre d’éléments (dans notre exemple, plus de 180’000), ce qui est simplement infaisable avec les méthodes visuelles.
Il faut donc en passer par les scripts et PowerShell + CSOM est parfait pour ce type de gestion.
Voici donc un script permettant le nettoyage basé sur la date limite, ce qui veut dire supprimer tous les élément avant la date du 4 Janvier 2017.
[string]$username = "YourTenantAccount@yourtenant.onmicrosoft.com"
[string]$PwdTXTPath = "C:\FOLDERTOSTOREPWD\ExportedPWD-$($username).txt"[string]$SiteCollectionToClean = https://YourTenant.sharepoint.com/sites/YourSiteColl
[string]$ListToCleancontent = "YourListName"
[string]$ItemDateLimitToDelete = "2017-01-04"
[int]$ItemLimitNumber = 0
[int]$MyRowLimit = 500$secureStringPwd = ConvertTo-SecureString -string (Get-Content $PwdTXTPath)
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwdfunction 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 QuickCleanAllListItems([string]$SiteURL, [string]$ListName, [int]$LimitItemNum)
{
$sw = New-Object System.Diagnostics.StopWatch
$sw.Start()$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($creds.UserName,$creds.Password)
$ctx.RequestTimeout = 1000000 # millisecondsWrite-Host " ---------------------------------------------------------------------------------"
$spoWeb = $ctx.Web
$ctx.Load($spoWeb)
$ctx.ExecuteQuery()$MyListToClean = $spoWeb.Lists.GetByTitle($ListName)
$ctx.Load($MyListToClean)
$ctx.ExecuteQuery()
Write-Host "Site URL: ", $SiteURL
Write-Host "List Name:", $MyListToClean.TitleWrite-Host " Items Number before delete: ", $MyListToClean.ItemCount -foregroundcolor Yellow
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.DatesInUtc = $true
$query.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='FALSE'>$($ItemDateLimitToDelete)</Value></Eq></Where><OrderBy><FieldRef Name='Created'/></OrderBy></Query><RowLimit>1</RowLimit></View>"$itemLimitList = $MyListToClean.GetItems($query)
$ctx.Load($itemLimitList)
$ctx.ExecuteQuery()if($itemLimitList.Count -gt 0)
{
$ItemLimitNumber = $itemLimitList[$itemLimitList.Count-1].ID
[int]$looplimitcount
[int]$LoopRound = 0
DO
{
Write-Host " ========================================="
Write-Host " Deletion Round Number: ", $LoopRound -foregroundcolor Red$queryclear = New-Object Microsoft.SharePoint.Client.CamlQuery
$queryclear.DatesInUtc = $true
$queryclear.ViewXml = "<View><Query><Where><Lt><FieldRef Name='ID'/><Value Type='Counter'>$ItemLimitNumber</Value></Lt></Where><OrderBy><FieldRef Name='ID'/></OrderBy></Query><RowLimit>$MyRowLimit</RowLimit></View>"
$items = $MyListToClean.GetItems($queryclear)
$ctx.Load($items)
$ctx.ExecuteQuery()
$looplimitcount = $items.Count
if ($items.Count -gt 0)
{
for ($i=$items.Count-1; $i-ge0; $i--)
{
Write-Host " >> Item ID: ", $items[$i].ID, " ====>> Deleted" -foregroundcolor Magenta
$items[$i].DeleteObject()
}
$ctx.ExecuteQuery()
Write-Host " ====>> Update Applied !!!" -foregroundcolor Red
}
$LoopRound += 1
} WHILE ( $looplimitcount -gt 0)}
$sw.Stop()Write-Host " ---------------------------------------------------------------------------------"
$ctx.Load($MyListToClean)
$ctx.ExecuteQuery()
Write-Host "Items total after deletion: ", $MyListToClean.ItemCount -foregroundcolor Yellow
write-host "Items deleted in " $sw.Elapsed.ToString()}
Load-DLLandAssemblies
QuickCleanAllListItems $SiteCollectionToClean $ListToCleancontent $ItemNumberLimitToDelete
Il ne vous reste plus qu’à adapter les critères de recherche si vous souhaitez nettoyer selon un autre filtre via la partie CAMLQuery.
Pour ma part, les 180’000 items ont été supprimés en quelques heures au lieu de plusieurs jours si on passe par les écrans de base.
Liens en rapport avec ce script:
- http://www.c-sharpcorner.com/code/3074/delete-list-items-csom-sharepoint-online.aspx
- https://karinebosch.wordpress.com/2012/02/03/caml-and-the-client-object-model/
- https://blogs.technet.microsoft.com/heyscriptingguy/2014/05/05/powershell-looping-understanding-and-using-do-while/
- http://blogs.developpeur.org/fabrice69/archive/2016/10/18/office-365-comment-viter-de-taper-son-mot-de-passe-du-tenant-dans-les-scripts-powershell.aspx
Fabrice Romelard [MBA Risk Management]
Commentaires
Enregistrer un commentaire