Last active
August 29, 2015 14:01
-
-
Save pkskelly/f2df1d905ee266abb323 to your computer and use it in GitHub Desktop.
Snippet from an old script which deletes documents from a SharePoint Document library using SPWeb.ProcessBatchData(). Script used a CSV file of $activities which defined what should be deleted.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ($clean) | |
{ | |
$deletedSharedDocs = $false; | |
Write-Host "`n`nCleaning all lists of all content...Please wait" | |
foreach($activity in $activities) | |
{ | |
$web = get-web $activity.RootUrl | |
$list = $web.Lists[$activity.ListName]; | |
\ if ($list.BaseType -eq "DocumentLibrary" -and ($list.ItemCount -gt 0)) | |
{ | |
if ($deletedSharedDocs -eq $false) | |
{ | |
Write-Host "Building Batch Delete Query..."; | |
$strBld = new-object System.Text.StringBuilder; | |
$strBld.Append("<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch OnError=`"Return`">"); | |
$list.Items | % { $strBld.Append("<Method>"); | |
$strBld.Append("<SetList Scope=`"Request`">"); | |
$strBld.Append($list.ID.ToString()); | |
$strBld.Append("</SetList>"); | |
$strBld.Append("<SetVar Name=`"ID`">"); | |
$strBld.Append($_.ID.ToString()); | |
$strBld.Append("</SetVar>"); | |
$strBld.Append("<SetVar Name=`"Cmd`">Delete</SetVar>"); | |
$strBld.Append("<SetVar Name=`"owsfileref`">"); | |
$strBld.Append($_.File.ServerRelativeUrl); | |
$strBld.Append("</SetVar>"); | |
$strBld.Append("</Method>"); | |
} | |
$strBld.Append("</Batch>"); | |
Write-Host "Processing Batch Delete..." | |
$result = $web.ProcessBatchData($strBld.ToString()); | |
Write-Debug $result | |
$deletedSharedDocs = $true; | |
} | |
} | |
else | |
{ | |
if ($list.ItemCount -gt 0) | |
{ | |
Write-Debug "Deleting : $list.ItemCount from $list.Title"; | |
$list.Folders| % { $_.Delete()} | |
$list.Items | % { $_.Delete()} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment