Bulk Add/Update/ Delete Share point List Items
Bulk Adding :
#region [ Method : Batch Add List item details ]
public void AddCanteenMenuDetails(string lstName, List<CanteenMenuDetails> objCanteenMenuDetails)
{
StringBuilder sbInsertCanteenMenu = new StringBuilder();
sbInsertCanteenMenu.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPList lstCanteenMenu = web.Lists.TryGetList(lstName);
bool allowUnsafeUpdates = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;
web.Update();
foreach (CanteenMenuDetails item in objCanteenMenuDetails)
{
sbInsertCanteenMenu.AppendFormat("<Method ID=\"{0}\">" +
"<SetList>{1}</SetList>" +
"<SetVar Name=\"ID\">New</SetVar>" +
"<SetVar Name=\"Cmd\">Save</SetVar>" +
"<SetVar Name=\"{3}Title\">{2}</SetVar>" +
"<SetVar Name=\"{3}Location\">{4}</SetVar>" +
"<SetVar Name=\"{3}WeekDays\">{5}</SetVar>" +
"</Method>", item.Title, lstCanteenMenu.ID, item.Title, "urn:schemas-microsoft-com:office:office#", item.LocationID, item.WeekDays);
}
sbInsertCanteenMenu.Append("</Batch>");
web.ProcessBatchData(sbInsertCanteenMenu.ToString());
web.AllowUnsafeUpdates = allowUnsafeUpdates;
web.Update();
}
}
}
#endregion
Bulk Updating:
#region [ Method : Batch Update List items details ]
public void UpdateCanteenMenuDetails(string lstName, List<CanteenMenuDetails> objCanteenMenuDetails)
{
StringBuilder sbUpdateCanteenMenu = new StringBuilder();
string batchFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ows:Batch OnError=\"Return\">{0}</ows:Batch>";
string methodFormat = "<Method ID=\"{0}\">" +
"<SetList>{1}</SetList>" +
"<SetVar Name=\"Cmd\">{2}</SetVar>" +
"<SetVar Name=\"ID\">{3}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Breakfast\">{4}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Lunch\">{5}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Dinner\">{6}</SetVar>" +
"</Method>";
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
bool allowUnsafeUpdates = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;
web.Update();
SPList lstCanteenMenu = web.Lists.TryGetList(lstName);
foreach (CanteenMenuDetails item in objCanteenMenuDetails)
{
sbUpdateCanteenMenu.AppendFormat(methodFormat, item.ID, lstCanteenMenu.ID.ToString(), "Save", item.ID, item.Breakfast,item.Lunch,item.Dinner);
}
//batch update script.
string batchUpdateScript = string.Format(batchFormat, sbUpdateCanteenMenu.ToString());
web.ProcessBatchData(batchUpdateScript);
web.AllowUnsafeUpdates = allowUnsafeUpdates;
web.Update();
}
}
}
#endregion
Bulk Deleting :
#region [Method : Batch Delete List item details ]
public void DeleteCanteenMenu(string lstName, List<CanteenMenuDetails> objCanteenMenuDetails)
{
StringBuilder deletebuilder = new StringBuilder();
deletebuilder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
//SPSite site = SPContext.Current.Site;
//SPWeb web = site.OpenWeb();
bool allowUnsafeUpdates = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;
web.Update();
SPList lstList = web.Lists.TryGetList(lstName);
if (lstList != null)
{
// IEnumerable<SPListItem> lstItem = lstList.GetItems(query).OfType<SPListItem>();
string command = "<Method><SetList Scope=\"Request\">" + lstList.ID +
"</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";
foreach (CanteenMenuDetails item in objCanteenMenuDetails)
{
deletebuilder.Append(string.Format(command, item.ID.ToString()));
}
deletebuilder.Append("</Batch>");
web.ProcessBatchData(deletebuilder.ToString());
web.Update();
}
}
}
}
#endregion
Bulk Adding :
#region [ Method : Batch Add List item details ]
public void AddCanteenMenuDetails(string lstName, List<CanteenMenuDetails> objCanteenMenuDetails)
{
StringBuilder sbInsertCanteenMenu = new StringBuilder();
sbInsertCanteenMenu.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPList lstCanteenMenu = web.Lists.TryGetList(lstName);
bool allowUnsafeUpdates = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;
web.Update();
foreach (CanteenMenuDetails item in objCanteenMenuDetails)
{
sbInsertCanteenMenu.AppendFormat("<Method ID=\"{0}\">" +
"<SetList>{1}</SetList>" +
"<SetVar Name=\"ID\">New</SetVar>" +
"<SetVar Name=\"Cmd\">Save</SetVar>" +
"<SetVar Name=\"{3}Title\">{2}</SetVar>" +
"<SetVar Name=\"{3}Location\">{4}</SetVar>" +
"<SetVar Name=\"{3}WeekDays\">{5}</SetVar>" +
"</Method>", item.Title, lstCanteenMenu.ID, item.Title, "urn:schemas-microsoft-com:office:office#", item.LocationID, item.WeekDays);
}
sbInsertCanteenMenu.Append("</Batch>");
web.ProcessBatchData(sbInsertCanteenMenu.ToString());
web.AllowUnsafeUpdates = allowUnsafeUpdates;
web.Update();
}
}
}
#endregion
Bulk Updating:
#region [ Method : Batch Update List items details ]
public void UpdateCanteenMenuDetails(string lstName, List<CanteenMenuDetails> objCanteenMenuDetails)
{
StringBuilder sbUpdateCanteenMenu = new StringBuilder();
string batchFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ows:Batch OnError=\"Return\">{0}</ows:Batch>";
string methodFormat = "<Method ID=\"{0}\">" +
"<SetList>{1}</SetList>" +
"<SetVar Name=\"Cmd\">{2}</SetVar>" +
"<SetVar Name=\"ID\">{3}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Breakfast\">{4}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Lunch\">{5}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Dinner\">{6}</SetVar>" +
"</Method>";
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
bool allowUnsafeUpdates = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;
web.Update();
SPList lstCanteenMenu = web.Lists.TryGetList(lstName);
foreach (CanteenMenuDetails item in objCanteenMenuDetails)
{
sbUpdateCanteenMenu.AppendFormat(methodFormat, item.ID, lstCanteenMenu.ID.ToString(), "Save", item.ID, item.Breakfast,item.Lunch,item.Dinner);
}
//batch update script.
string batchUpdateScript = string.Format(batchFormat, sbUpdateCanteenMenu.ToString());
web.ProcessBatchData(batchUpdateScript);
web.AllowUnsafeUpdates = allowUnsafeUpdates;
web.Update();
}
}
}
#endregion
Bulk Deleting :
#region [Method : Batch Delete List item details ]
public void DeleteCanteenMenu(string lstName, List<CanteenMenuDetails> objCanteenMenuDetails)
{
StringBuilder deletebuilder = new StringBuilder();
deletebuilder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
//SPSite site = SPContext.Current.Site;
//SPWeb web = site.OpenWeb();
bool allowUnsafeUpdates = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;
web.Update();
SPList lstList = web.Lists.TryGetList(lstName);
if (lstList != null)
{
// IEnumerable<SPListItem> lstItem = lstList.GetItems(query).OfType<SPListItem>();
string command = "<Method><SetList Scope=\"Request\">" + lstList.ID +
"</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";
foreach (CanteenMenuDetails item in objCanteenMenuDetails)
{
deletebuilder.Append(string.Format(command, item.ID.ToString()));
}
deletebuilder.Append("</Batch>");
web.ProcessBatchData(deletebuilder.ToString());
web.Update();
}
}
}
}
#endregion