SPWebProcessBatchData批量操作
The following code example uses the ProcessBatchData method to add two items to the Announcements list of a specified site in the current site collection.
using (SPWeb oWebsite = SPContext.Current.Site.OpenWeb("Website_URL"))
{
SPList oList = oWebsite.Lists["Announcements"];
System.Guid guid = oList.ID;
string strGuid = guid.ToString();
string strPost = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ows:Batch OnError=\"Return\">" +
"<Method ID=\"A1\"><SetList>" + strGuid + "</SetList>" +
"<SetVar Name=\"ID\">New</SetVar>" +
"<SetVar Name=\"Cmd\">Save</SetVar>" +
"<SetVar Name=" +
"\"urn:schemas-microsoft-com:office:office#Title\">" +
"New Manager</SetVar>" +
"<SetVar Name=" +
"\"urn:schemas-microsoft-com:office:office#Body\">" +
"Congratulations to Mary for her promotion!</SetVar>" +
"<SetVar Name=" +
"\"urn:schemas-microsoft-com:office:office#Expires\">" +
"2003-09-14T00:00:00Z</SetVar>" +
"</Method>" +
"<Method ID=\"A2\">" +
"<SetList>" + strGuid + "</SetList>" +
"<SetVar Name=\"ID\">New</SetVar>" +
"<SetVar Name=\"Cmd\">Save</SetVar>" +
"<SetVar Name=" +
"\"urn:schemas-microsoft-com:office:office#Title\">" +
"New Technical Consultant</SetVar>" +
"<SetVar Name=" +
"\"urn:schemas-microsoft-com:office:office#Body\">" +
"Welcome to the team, John!</SetVar>" +
"<SetVar Name=" +
"\"urn:schemas-microsoft-com:office:office#Expires\">" +
"2007-10-15T00:00:00Z</SetVar>" +
"</Method>" +
"</ows:Batch>";
string strProcessBatch = oWebsite.ProcessBatchData(strPost);
}
Return :在第一次出错后不再执行后续的Methiod.
Continue :在出错后继续执行后续的Methiod.
ListVersion :List的版本号。(可选)
Version :SharePoint的版本号。(可选)
添加
- SPList list = web.Lists["Tasks"];
- String batch = String.Format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
- "<ows:Batch OnError=\"Continue\"><Method ID='1'>" +
- "<SetList>{0}</SetList>" +
- "<SetVar Name='Cmd'>Save</SetVar>" +
- "<SetVar Name='ID'>3</SetVar>" +
- "<SetVar Name='urn:schemas-microsoft-com:office:office#Location'>1;#;#2</SetVar>" +
- "<SetVar Name='urn:schemas-microsoft-com:office:office#Owners'>1;#;#7<</SetVar>" +
- "<SetVar Name='urn:schemas-microsoft-com:office:office#Choices'>Value1;#Value2</SetVar>" +
- "</Method></ows:Batch>", list.ID);
- String batchResult = list.ParentWeb.ProcessBatchData(batch);
- 更新
- MyListService.Credentials = CredentialCache.DefaultCredentials;
- MyListService.Url = "http://yourserver/_vti_bin/lists.asmx";
- XmlDocument updateRequest = new XmlDocument();
- String updateBatch = "<Batch OnError='Continue'>" +
- "<Method ID='1' Cmd='Update'>" +
- "<Field Name='ID'>2</Field>" +
- "<Field Name='Location'>1;#;#2</Field>" +
- "<Field Name='Owners'>1;#;#7</Field>" +
- "<Field Name='Choices'>Value1;#Value2</Field>" +
- "</Method>" +
- "</Batch>";
- updateRequest.LoadXml(updateBatch);
- XmlNode deleteResult = MyListService.UpdateListItems("Tasks", updateRequest.DocumentElement);
用item.Delete 来删除 ,如果行数在10多行的时候,删除所需要的时间会达到好几1-2秒,所以非常之慢!一般来说,当行数多的时候会采取以下的方法来删除
publicvoidDeleteData(SPListItemCollection items, SPList itemlist){SPSecurity.RunWithElevatedPrivileges(delegate(){using(SPSite site =newSPSite(itemlist.ParentWeb.Site.Url)){SPWeb web = site.OpenWeb(itemlist.ParentWeb.ID);SPList list = web.Lists[itemlist.ID];StringBuilder sbDelete =newStringBuilder();sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");foreach(SPListItem iteminitems){sbDelete.Append("<Method>");sbDelete.Append("<SetList Scope=\"Request\">"+ list.ID +"</SetList>");sbDelete.Append("<SetVar Name=\"ID\">"+ Convert.ToString(item.ID) +"</SetVar>");sbDelete.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");sbDelete.Append("</Method>");}sbDelete.Append("</Batch>");web.ProcessBatchData(sbDelete.ToString());}});}
浙公网安备 33010602011771号