Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

Delete multiple web parts in a SharePoint 2010 page programmatically.

$
0
0

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!!


Viewing all articles
Browse latest Browse all 11571

Trending Articles