<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Uncatchable Spark</title>
	<atom:link href="http://blog.nas.lv/en/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.nas.lv</link>
	<description>The More We Know, the Less We Understand</description>
	<lastBuildDate>Tue, 29 Sep 2009 21:05:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SPWeb.ProcessBatchData throws &#8220;Value does not fall within the expected range&#8221; exception</title>
		<link>http://blog.nas.lv/en/2009/09/29/spweb-processbatchdata-throws-value-does-not-fall-within-the-expected-range-exception/</link>
		<comments>http://blog.nas.lv/en/2009/09/29/spweb-processbatchdata-throws-value-does-not-fall-within-the-expected-range-exception/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 14:00:16 +0000</pubDate>
		<dc:creator>nas</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SharePoint]]></category>

		<guid isPermaLink="false">http://blog.nas.lv/?p=23</guid>
		<description><![CDATA[Recently I needed to delete a large number of items from List and I tried to solve it by simply invoking Delete() method on each SPListItem object.

Function Clear-List([Microsoft.SharePoint.SPList]$list) {
  $list.Items &#124; ForEach-Object { $_.Delete() }
}

That did the trick but I wasn’t really satisfied with the results – the code was deleting an item every [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I needed to delete a large number of items from List and I tried to solve it by simply invoking Delete() method on each SPListItem object.</p>
<pre class="brush: powershell;">
Function Clear-List([Microsoft.SharePoint.SPList]$list) {
  $list.Items | ForEach-Object { $_.Delete() }
}
</pre>
<p>That did the trick but I wasn’t really satisfied with the results – the code was deleting an item every 2-3 seconds. That made me looking for alternative approache and eventually I came across <a title="http://merill.net/2008/02/efficiently-delete-purge-all-items-from-a-sharepoint-list/" href="http://merill.net/2008/02/efficiently-delete-purge-all-items-from-a-sharepoint-list/">http://merill.net/2008/02/efficiently-delete-purge-all-items-from-a-sharepoint-list/</a>, it was looking like a solution to my problem, so I took the idea and built my own function using <strong>XmlTextWriter</strong> instead of <strong>StringBuilder</strong>.</p>
<p>When I tried to run it, I was surprised – it didn’t work. The <strong>ProcessBatchData</strong> method was throwing <strong>Value does not fall within the expected range</strong> exception for no apparent reason, so I switched back to <strong>StringBuilder</strong> method from original article and that surprised me again – it worked.</p>
<p>That intrigued me enough to start annihilating any difference between generated XML strings by both methods. I was shocked when during my very last iteration, I discovered the case of encoding declaration to be cause for this exception. Take a look by yourself:</p>
<p>A non working document declaration produced by XmlTextWriter.WriteStartDocument() method:</p>
<pre class="brush: xml;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</pre>
<p>A working document declaration produced by code from article:</p>
<pre class="brush: xml;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</pre>
<p>Looks like a bug with <strong>ProcessBatchData</strong> method – maybe someday I will open up a case with Microsoft but for now as a workaround we can simply either fallback to <strong>StringBuilder</strong> method or replace utf-8 with UTF-8 before invoking <strong>ProcessBatchData</strong> method.</p>
<pre class="brush: powershell;">
Function Clear-List([Microsoft.SharePoint.SPList]$list) {
  $requestStream = New-Object System.IO.MemoryStream
  $requestWriter = New-Object System.Xml.XmlTextWriter($requestStream, [System.Text.Encoding]::UTF8)

  $requestWriter.WriteStartDocument()
  $requestWriter.WriteStartElement(&quot;Batch&quot;)

  $list.Items | ForEach-Object {
    $requestWriter.WriteStartElement(&quot;Method&quot;)
    $requestWriter.WriteStartElement(&quot;SetList&quot;)
    $requestWriter.WriteAttributeString(&quot;Scope&quot;, &quot;Request&quot;)
    $requestWriter.WriteString($list.ID)
    $requestWriter.WriteEndElement()
    $requestWriter.WriteStartElement(&quot;SetVar&quot;)
    $requestWriter.WriteAttributeString(&quot;Name&quot;, &quot;ID&quot;)
    $requestWriter.WriteString($_.ID)
    $requestWriter.WriteEndElement()

    if($list.BaseType -eq [Microsoft.SharePoint.SPBaseType]::DocumentLibrary) {
      $requestWriter.WriteStartElement(&quot;SetVar&quot;)
      $requestWriter.WriteAttributeString(&quot;Name&quot;, &quot;owsfileref&quot;)
      $requestWriter.WriteString($_.File.ServerRelativeUrl)
      $requestWriter.WriteEndElement()
    }

    $requestWriter.WriteStartElement(&quot;SetVar&quot;)
    $requestWriter.WriteAttributeString(&quot;Cmd&quot;, &quot;Delete&quot;)
    $requestWriter.WriteEndElement()
    $requestWriter.WriteEndElement()
  }

  $requestWriter.WriteEndElement()
  $requestWriter.WriteEndDocument()

  $requestWriter.Flush()

  $requestStream.Seek(0, [System.IO.SeekOrigin]::Begin)

  $requestReader = New-Object System.IO.StreamReader($requestStream)
  $requestText = $requestReader.ReadToEnd()
  $requestStream.Close()
  $requestText = $requestText.Replace(&quot;utf-8&quot;, &quot;UTF-8&quot;)

  $result = $list.ParentWeb.ProcessBatchData($requestText)
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.nas.lv/en/2009/09/29/spweb-processbatchdata-throws-value-does-not-fall-within-the-expected-range-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
