Tuesday, September 8, 2009

SPWeb.ProcessBatchData. A list is a list is a list?

Technorati Tags: ,

 

There is a lot of material on the Internet about the Microsoft.SharePoint.SPWeb.ProcessBatchData method. It is a great method for processing a lot of commands against a SPList without having to open a SPListItemCollection and pay the penalty of slow performance if the list contains a substantial amount of items. The ProcessBatchData method seems to be used only by the Microsoft.SharePoint.SoapServer namespace (stssoap.dll) which is the assembly that implements the basic SharePoint webservices. For example, the lists webservice uses ProcessBatchData when processing requests to the UpdateListItems method. The UpdateListItems and the ProcessBatchData methods take the "Windows SharePoint RPC Protocol" formatted caml batch commands (http://msdn2.microsoft.com/en-us/library/ms448359.aspx). The point of this article is to clear up some confusion about the actual syntax of this caml and one of the undocumented syntax differences depending on what derived type of SPList you are operating against.

 

Calling UpdateListItems or ProcessBatchData?

The main focus is to delete a particular item in a list. I have seen many examples of the caml that is needed to accomplish this with the ProcessBatchData method.

 

Example One (Deleting a file from a SharePoint List) using ProcessBatchData

<?xml version="1.0" encoding="UTF-8"?>
<Batch>
<Method>
  <SetList Scope="Request">3010913d-9373-44ec-a799-0bf564cb0d66</SetList>
  <SetVar Name="Cmd">DELETE</SetVar>
  <SetVar Name="ID">1</SetVar>
</Method>
</Batch>

The above will delete a list item from a standard SharePoint list not a document library. You need to supply the guid of the SPList and the ID property of the SPListItem. Unfortunately this does not work with a document library. If you try using this with a document library you will get "The file name you specified could not be used. It may be the name of an existing file or directory, or you may not have permission to access the file."

 

Example Two (Deleting a file from a SharePoint Document Library) using ProcessBatchData

<Batch>
<Method>
<SetList Scope="Request">3010913d-9373-44ec-a799-0bf564cb0d66</SetList>
<SetVar Name="Cmd">DELETE</SetVar>
<SetVar Name="ID">1</SetVar>
<SetVar Name="owsfileref">sites/tester1/myfile.bmp</SetVar>
</Method>
</Batch>

 

The above will delete a list item from a SharePoint document library. You do not need to supply the ID property of the SPListItem but you must supply the item's "Url Path" field for the "owsfileref" variable. The internal name for the "Url Path" field is "FileRef". The ID variable in the caml is ignored and is basically used to number separate commands.

 

Example Three (Deleting a file from a SharePoint Document Library) using UpdateListItems

<?xml version="1.0" encoding="UTF-8"?>
<Batch>
<Method ID='1' Cmd='Delete'>
  <Field Name='ID'>1</Field>
  <Field Name='FileRef'>http://basesmcdev/sites/tester1/myfile.bmp</Field>
</Method>
</Batch>

 

Example three shows the caml to use when using the lists webservice. Yes it is different syntax. It uses the "Field" element instead of the "SetVar" element. Why? Not sure. This caml gets translated by the Microsoft.SharePoint.SoapServer.ListDataImpl.ConstructCaml method into the caml  listed in example two and then passed into the ProcessBatchData method.

 

In summary the Windows SharePoint RPC protocol caml is not documented very well,there are two syntaxes and special variable names that no one knows about like "owsfileref".  The good news is that through trial and error (Thomas Edison) you can figure out what works and what does not.  Good experimenting. 

2 comments:

Anonymous said...

HI,

how we can use processbatchdata to insert documents into document library

Anonymous said...

Thank you so much for this. Because of your blog, I was able to figure out why my document library updates were not working using the ProcessBatchData method.

Post a Comment