Batch Update In SharePoint
SharePoint support batch update in List by webservice and SharePoint object model.
For Example i used sample Custom List (Orders).

1. By Use of Web Service.
SharePoint webservice allows update list items as a batch.At a time only one list can be updated by single webservice call.
CAML That use for Batch Update. Here i update item with ID 2 and create new item in Orders List in one webservice call.
2
2007-1-21
1;#Cust_1
2007-1-21
2;#Cust_2
C# Code (Either Add web reference of SharePoint webservice or Create Proxy Class using wsdl.exe and use that class)
Lists lst = new Lists();
lst.Url = “http:///_vti_bin/lists.asmx”;
lst.Credentials = new System.Net.NetworkCredential ("test", "test");
XmlDocument doc = new XmlDocument();
XmlElement batchElement = doc.CreateElement("Batch");
batchElement.SetAttribute("OnError", "Continue");
batchElement.InnerXml = "" +
"2 " +
"2009-1-31 2009-1-31 1;#Cust_1 2009-1-31 2009-1-31 ";
XmlNode result = lst.UpdateListItems("Orders", batchElement); // This line of code make webservice call to update lists.
One more thing In CAML displayed above has two bold line for CustomerID field. CustomerID is a LookUp Field. So i mention that date id;#Value (Commom lookup Structure).If you want to choose lookup value only by id then use following CAML for Field.
2
2007-1-21
1
2007-1-21
2
2. By Use Of SharePoint Object Model. (SPWeb.ProcessBatchData)
SharePoint Object Model has SPWeb class. SPWeb class has ProcessBatchData Method , this method is used to update Batch via object model. Even this method allows you update two list items in Single method call.
Sample CAML .
71a9fac3-2e94-4246-8fec-41e30ea65b06
Save
New
2009-2-21
2009-2-21
1;#Cust_1
71a9fac3-2e94-4246-8fec-41e30ea65b06
Save
3
2009-2-21
2009-2-21
1;#Cust_1
71a9fac3-2e94-4246-8fec-41e30ea65b06
Delete
4
2009-2-21
2009-2-21
2;#Cust_2
In above SetList element set GUID of SPList. Here i only update Orders List but you can set another list id combine it with above by just adding another Method Element.
SetVar element with attribute Name ‘cmd’ used to identify type of functionality. It has mainly two values : Save and Delete . Save is used for either for New Item or in case of update existing item.
SetVar element with attribute Name ‘ID’ used to find out on which item it operate. ID value ‘New’ is for new item and in case of Update and Delete it should have valid id value. (Integer).
SetVar element with Name attribute value urn:schemas-microsoft-com:office:office# indicate perticular field of ListItem, should replace by respetive field internal name.
C# Code.
SPSite site = new SPSite("http://avani:45830");
SPWeb web = site.OpenWeb();
SPList lst = web.Lists["Orders"];
string batchFormat = "" +
"{0} ";
string methodFormat = "" +
"{1} " +
"{2} " +
"{3} " +
"{4} " +
"{5} " +
"{6} " +
" ";
StringBuilder strmethodFormat = new StringBuilder();
strmethodFormat.AppendFormat(methodFormat, 1, lst.ID.ToString(), "Save", "New", "2009-2-21", "2009-2-21","1;#Cust_1");
strmethodFormat.AppendFormat(methodFormat, 2, lst.ID.ToString(), "Save", "3", "2009-2-21", "2009-2-21","1;#Cust_1");
strmethodFormat.AppendFormat(methodFormat, 3, lst.ID.ToString(), "Save", "4", "2009-2-21", "2009-2-21","2;#Cust_2");
string processtext = String.Format(batchFormat, strmethodFormat.ToString());
web.ProcessBatchData(processtext);
For Example i used sample Custom List (Orders).
1. By Use of Web Service.
SharePoint webservice allows update list items as a batch.At a time only one list can be updated by single webservice call.
CAML That use for Batch Update. Here i update item with ID 2 and create new item in Orders List in one webservice call.
C# Code (Either Add web reference of SharePoint webservice or Create Proxy Class using wsdl.exe and use that class)
Lists lst = new Lists();
lst.Url = “http://
lst.Credentials = new System.Net.NetworkCredential ("test", "test");
XmlDocument doc = new XmlDocument();
XmlElement batchElement = doc.CreateElement("Batch");
batchElement.SetAttribute("OnError", "Continue");
batchElement.InnerXml = "
"
"
XmlNode result = lst.UpdateListItems("Orders", batchElement); // This line of code make webservice call to update lists.
One more thing In CAML displayed above has two bold line for CustomerID field. CustomerID is a LookUp Field. So i mention that date id;#Value (Commom lookup Structure).If you want to choose lookup value only by id then use following CAML for Field.
2. By Use Of SharePoint Object Model. (SPWeb.ProcessBatchData)
SharePoint Object Model has SPWeb class. SPWeb class has ProcessBatchData Method , this method is used to update Batch via object model. Even this method allows you update two list items in Single method call.
Sample CAML .
In above SetList element set GUID of SPList. Here i only update Orders List but you can set another list id combine it with above by just adding another Method Element.
SetVar element with attribute Name ‘cmd’ used to identify type of functionality. It has mainly two values : Save and Delete . Save is used for either for New Item or in case of update existing item.
SetVar element with attribute Name ‘ID’ used to find out on which item it operate. ID value ‘New’ is for new item and in case of Update and Delete it should have valid id value. (Integer).
SetVar element with Name attribute value urn:schemas-microsoft-com:office:office#
C# Code.
SPSite site = new SPSite("http://avani:45830");
SPWeb web = site.OpenWeb();
SPList lst = web.Lists["Orders"];
string batchFormat = "" +
"
string methodFormat = "
"
"
"
"
"
"
"
StringBuilder strmethodFormat = new StringBuilder();
strmethodFormat.AppendFormat(methodFormat, 1, lst.ID.ToString(), "Save", "New", "2009-2-21", "2009-2-21","1;#Cust_1");
strmethodFormat.AppendFormat(methodFormat, 2, lst.ID.ToString(), "Save", "3", "2009-2-21", "2009-2-21","1;#Cust_1");
strmethodFormat.AppendFormat(methodFormat, 3, lst.ID.ToString(), "Save", "4", "2009-2-21", "2009-2-21","2;#Cust_2");
string processtext = String.Format(batchFormat, strmethodFormat.ToString());
web.ProcessBatchData(processtext);
No comments:
Post a Comment