Thursday, February 12, 2015
Adding data to list in a Batch in SharePoint
SharePoint provide a functionality to perform CRUD operation in "bulk" with SPWeb's Batch Process methods. Below is the sample to ADD items in bulk.
//Hard Coded e.g. This I have just describe to learn the CAML query thoroughly.
<ows:Batch OnError="Return">1</ows:Batch>//1=itemID.
<Method ID="1">//1=itemID. Note - itemID is in double quotes.
<SetList>2222-22222222-2222</SetList>//ListID
<SetVar Name="Cmd">Save</SetVar>
<SetVar Name="ID">1</SetVar>//1=itemID
<SetVar Name="urn:schemas-microsoft-com:office:office#Title">Title 1</SetVar>
</Method>
//Dynamic Code e.g.
<ows:Batch OnError="Return">{0}</ows:Batch>//1=itemID.
<Method ID=\"{0}\">//1=itemID. Note - itemID is in double quotes.
<SetList>{1}</SetList>//ListID
<SetVar Name="Cmd">Save</SetVar>//Use Delete keyword to delete item, Save keyword to update item
<SetVar Name="ID">{2}</SetVar>//1=itemID
<SetVar Name="urn:schemas-microsoft-com:office:office#Title">{3}</SetVar>
</Method>
---------------------------------------------------------------------------------------------------------
Real time application e.g. with bulk updates.
using (SPSite site = new SPSite(gstrURL))
{
using (SPWeb web = site.OpenWeb())
{
SPList listBatch = web.Lists["BatchTest"];
string lstrBatchFormat = string.Empty, lstrMethodFormat = string.Empty;
StringBuilder methodBuilder = new StringBuilder();
lstrBatchFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ows:Batch OnError=\"Return\">{0}</ows:Batch>";
lstrMethodFormat ="<Method><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#aaa\">" +
"{3}</SetVar>" +
"</Method>";
for (int i = 0; i < listBatch.Items.Count; i++)
{
SPListItem item =listBatch.Items[i];
methodBuilder.AppendFormat(lstrMethodFormat, listBatch.Items.Count++, listBatch.ID.ToString(), "Title-" + i, "aaa-" + i);
}
string lstrBulkBatch = string.Format(lstrBatchFormat,methodBuilder);
string lstrResult = web.ProcessBatchData(lstrBulkBatch);
}
}
MSDN Reference:
Article 1
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment