1

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;
3

Your String.Format string has 3 tokens: {0}, {1}, and {2}

The method then expects you to pass 3 variables in addition to the main string to format.

However, you only pass 2: $listId, "{0}"

In your example:

$listID would map to {0}
"{0}" would map to {1}
nothing maps to {2}

See http://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx for more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.