I am trying to write a batch Powershell script to delete the items from Sharepoint Document Library.
I could not delete them because I have to reference owsfileRef field in batch command as per the below forum.
http://sharepointnadeem.blogspot.com/2013/01/bulk-delete-items-in-sharepoint.html.
I have build my powershell command I get the follwoing exception.
Can somebody see what is being missed please?
calling "Format" with "3" argument(s): "Index (zero based) must be greater than or equal
d less than the size of the argument list."
BatchDelete.ps1:12 char:35
= [System.String]::Format <<<< ( "{0}{1}<
etVar Name="FileRef
">{2}Delete", $listI
;
goryInfo : NotSpecified: (:) [], MethodInvocationException
yQualifiedErrorId : DotNetMethodException
My Code:
$list=$web.Lists["Shared Documents"]
$listname = $list.title
$query = New-Object Microsoft.SharePoint.SPQuery;
$query.ViewAttributes = "Scope='Recursive'";
$query.RowLimit = 2;
$query.Query = '<Where><And><And>
<Contains><FieldRef Name="File_x0020_Type"/><Value Type="Text">msg</Value></Contains><IsNotNull><FieldRef Name="CheckoutUser"/></IsNotNull></And>
<Eq><FieldRef Name="ContentType"/><Value Type="Text">ABCDoc</Value></Eq></And></Where>'
$itemCount = 0;
[System.Text.StringBuilder]$batchXml = New-Object "System.Text.StringBuilder";
$batchXml.Append("<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>");
$command = [System.String]::Format( "<Method><SetList>{0}</SetList><SetVar Name=`"ID`">{1}</SetVar><SetVar Name=`"FileRef`">{2}</SetVar><SetVar Name=`"Cmd`">Delete</SetVar></Method>", $listId, "{0}" );
do
{
$listItems = $list.GetItems($query)
$query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
foreach ($item in $listItems)
{
if($item -ne $null)
{
$batchXml.Append([System.String]::Format($command, $item.ID.ToString(),$item.File.ServerRelativeUrl.ToString()))| Out-Null;$itemCount++;
}
}
}
while ($query.ListItemCollectionPosition -ne $null)
$batchXml.Append("</Batch>");
write-host $batchXML.ToString()
$itemCount;
$web = Get-SPWeb "http://XXXXXX:80"
$web.ProcessBatchData($batchXml.ToString()) | Out-Null;
I made a mistake in string format..
Updated Query:
$list=$web.Lists["Shared Documents"]
$listname = $list.title
$query = New-Object Microsoft.SharePoint.SPQuery;
$query.ViewAttributes = "Scope='Recursive'";
$query.RowLimit = 2;
$query.Query = '<Where><And><And>
<Contains><FieldRef Name="File_x0020_Type"/><Value Type="Text">msg</Value></Contains><IsNotNull><FieldRef Name="CheckoutUser"/></IsNotNull></And>
<Eq><FieldRef Name="ContentType"/><Value Type="Text">ABCDOC</Value></Eq></And></Where>'
$itemCount = 0;
[System.Text.StringBuilder]$batchXml = New-Object "System.Text.StringBuilder";
$batchXml.Append("<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>");
$command = [System.String]::Format("<Method><SetList>{0}</SetList><SetVar Name=`"ID`">{0}</SetVar><SetVar Name=`"owsfileref`">{1}</SetVar><SetVar Name=`"Cmd`">Delete</SetVar></Method>", $listId, "{0}","{1}");
do
{
$listItems = $list.GetItems($query)
$query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
foreach ($item in $listItems)
{
if($item -ne $null)
{
$batchXml.Append([System.String]::Format($command, $item.ID.ToString(),$item.File.ServerRelativeUrl))| Out-Null;$itemCount++;
}
}
}
while ($query.ListItemCollectionPosition -ne $null)
$batchXml.Append("</Batch>");
write-host $batchXML.ToString()
$itemCount;
$web = Get-SPWeb "http://xxxxxxx:80"
$web.ProcessBatchData($batchXml.ToString()) | Out-Null;