Hello,
I'm looking for a solution to delete multiple web parts in a SharePoint page through code. The code is deleting the first web part and then throws an exception "Collection was modified; enumeration operation may not execute."
Any Suggestions?
Please find below my code.
SPFile file = web.GetFile("URL");
using (SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
foreach (System.Web.UI.WebControls.WebParts.WebPart wp in wpm.WebParts)
{
if (wp.Title == "WP1" || wp.Title == "WP2" || wp.Title == "WP3"
|| wp.Title == "WP4")
{
wpm.DeleteWebPart(wp);
web.Update();
}
}
Thanks in advance!!