Process Batch Data in SharePoint

In this post, I would like to illustrate how to use XML based Process Batch Data to a SharePoint list. Using the XML based batch data, it is relatively easier to update a large volume of data/items on a SharePoint list. Here, I am launching a CAML query on SharePoint list (where ‘Status’ column contains ‘woohoo’) to return a SPListItemCollection. Once the relevant list items are returned, then using the web.ProcessBatchData, I update the SharePoint list (‘Status’ column with ‘woohoo’ to ‘yahoo’).

Private Sub ProcessBatchData()

Dim siteCollectionUri As Uri = New Uri("urlofsitewithdesiredlist")
Using oSite As SPSite = New SPSite(siteCollectionUri.ToString)
Using oWeb As SPWeb = oSite.OpenWeb
Dim _list As SPList = oWeb.Lists("ListName")
Dim _items As SPListItemCollection = GetItems(_list, "Woohoo")
If _items IsNot Nothing Then
RunBatchDataProcess(oWeb, _items)
End If
End Using
End Using
End Sub

Private Function GetItems(ByVal _List As SPList, ByVal CAMLQueryTerm As String) As SPListItemCollection
Dim _Qry As SPQuery = New SPQuery
_Qry.Query = "<Where>" & _
"<Eq>" & _
"<FieldRef Name='Status' />" & _
"<Value Type='Choice'>" & CAMLQuery Term & "</Value>" & _
"</Eq>" & _
"</Where>"

Dim Results As SPListItemCollection = _List.GetItems(_Qry)
If Results.Count.Equals(0) Then
Return Nothing
Else
Return Results
End If
End Function

Private Sub RunBatchDataProcess(ByVal oWeb As SPWeb, ByVal _Items As SPListItemCollection)
If Not _Items.Count.Equals(0) Then
Dim BatchCmd As String = String.Empty
Dim BatchString As StringBuilder = New StringBuilder
Dim guid As String = _Items.List.ID.ToString

Dim BatchFormat As String = "<?xml version=""1.0"" encoding=""UTF-8""?>" + _
"<ows:Batch OnError=""Return"">{0}</ows:Batch>"

Dim MethodFormat As String = "<Method ID=""{0}""><SetList>{1}</SetList>" + _
"<SetVar Name=""Cmd"">Save</SetVar>" + _
"<SetVar Name=""ID"">{2}</SetVar>" + _
"<SetVar Name=""urn:schemas-microsoft-com:office:office#Status"">{3}</SetVar>" + _
"</Method>"

'Construct Caml for the XML above
For i As Integer = 0 To _Items.Count - 1
Dim id As Integer = _Items(i).ID
BatchString.AppendFormat(MethodFormat, id, guid, id, "Yahoo")
Next

BatchCmd = String.Format(BatchFormat, BatchString.ToString)

' Process batch updates
Try
oWeb.ProcessBatchData(BatchCmd)
Catch ex As Exception
Throw ex
End Try
Else
Exit Sub
End If
End Sub

Other instances where one could utilise ProcessBatchData are if an existing SharePoint list needs to be copied to a new list to new columns and I am sure there are many occassions this would come handy.

Reference:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.processbatchdata.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s