Skip to content

Instantly share code, notes, and snippets.

@pkskelly
Last active August 29, 2015 14:01
Show Gist options
  • Save pkskelly/f2df1d905ee266abb323 to your computer and use it in GitHub Desktop.
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.
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