SharePoint List Batch Update
I was required to add and delete nearly 100 items (it may grow) in a list. If we do it in normal procedure we will definitely face performance issue and also there is a chance of Request timed out error. Calling SPListItem.Update() or SPListItem.Delete() method for every record is not recommended, because every time it hits the database.
It just takes one string parameter and it is a CAML string contains batch string of commands, which consists of a batch element. With following example you will get to know how to use this.
<SetVar Name=\"RootFolder\">/Lists/TestList/2012</SetVar>
You can also create folders in following ways.
string methodFormat = "<Method ID=\"{0}\">" +
"<SetList>{1}</SetList>" +
"<SetVar Name=\"ID\">New</SetVar>" +
"<SetVar Name=\"Cmd\">Save</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Title\">{2}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#ContentType\">Folder</SetVar>" +
"</Method>";
another way:
<?xml version="1.0" encoding="utf-8"?>
<ows:Batch OnError="Continue">
<Method ID="Test">
<SetList Scope="Request">82d62a9a-55ba-49c8-a9b8-68ec965a5931</SetList>
<SetVar Name="Cmd">Save</SetVar>
<SetVar Name="ID">New</SetVar>
<SetVar Name="Type">1</SetVar>
<SetVar Name="owsfileref">/sites/1/docs/folder1</SetVar>
</Method>
</ows:Batch>
- SPList lstData = web.GetList(web.Url + "/Lists/Ratio");
- SPListItemCollection itmColl = lstData.GetItems(lstData.DefaultView);
- //assume the datatable dtData have 100 records
- foreach(DataRow dr in dtData.Rows)
- {
- SPListItem itemData = itmColl.Add();
- itemData["Title"] = dr["title"].ToString();
- itemData["PaperCount"] = dr["PaperCount"].ToString();
- itemData.Update();
- }
It just takes one string parameter and it is a CAML string contains batch string of commands, which consists of a batch element. With following example you will get to know how to use this.
- StringBuilder sbAOERatio = new StringBuilder();
- sbAOERatio.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
- string command = "<Method><SetList Scope=\"Request\">{0}</SetList>" +
- "<SetVar Name=\"ID\">New</SetVar>" +
- "<SetVar Name=\"Cmd\">Save</SetVar>" +
- "<SetVar Name=\"urn:schemas-microsoft-com:office:office#Title\">{1}</SetVar>" +
- "<SetVar Name=\"urn:schemas-microsoft-com:office:office#PaperCount\">{2}</SetVar>" +
- "<SetVar Name=\"urn:schemas-microsoft-com:office:office#ReviewerCount\">{3}</SetVar>" +
- "<SetVar Name=\"urn:schemas-microsoft-com:office:office#Ratio\">{4}</SetVar>" +
- "</Method>";
- foreach (DataRow dr in dtData.Rows)
- {
- sbAOERatio.AppendFormat(command, lstAOERatio.ID, Convert.ToString(dr["title"]), P, R, ratio);
- }
- sbAOERatio.Append("</Batch>");
- web.ProcessBatchData(sbAOERatio.ToString());
Give the exact list item ID to update instead of New.
If you want to perform delete operation, change the step-5. Give Delete instead of Save command.
The above batch command will add the items in Root folder. In case, if you are required to add items in specific folder check the below code.
You can also create folders in following ways.
string methodFormat = "<Method ID=\"{0}\">" +
"<SetList>{1}</SetList>" +
"<SetVar Name=\"ID\">New</SetVar>" +
"<SetVar Name=\"Cmd\">Save</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Title\">{2}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#ContentType\">Folder</SetVar>" +
"</Method>";
another way:
<?xml version="1.0" encoding="utf-8"?>
<ows:Batch OnError="Continue">
<Method ID="Test">
<SetList Scope="Request">82d62a9a-55ba-49c8-a9b8-68ec965a5931</SetList>
<SetVar Name="Cmd">Save</SetVar>
<SetVar Name="ID">New</SetVar>
<SetVar Name="Type">1</SetVar>
<SetVar Name="owsfileref">/sites/1/docs/folder1</SetVar>
</Method>
</ows:Batch>
This way you can create subfolders too, e.g. using:
<SetVar Name="owsfileref">/sites/1/docs/folder1/sub1</SetVar>
Great
ReplyDeletegood Job man!
ReplyDeletehow to add hyperlink and publishing field using batch
ReplyDelete