I want to delete some of the webparts from my sites when they are created. I've stapled a feature that runs custom code. I've attached the basics of what it does below, edited for brevity.
It works but when you create a my site you get the error message "The operation could not be completed because the Web Part was deleted by another user or is invalid.", I'd guess because it's trying to load the webparts that the background thread is halfway through deleting. Refresh and the page loads fine.
Any ideas how to get around this?
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
thread.Start(deletethestuff)
}
public override void DeleteTheStuff
{
while (checksiteisprovisioned == false) Thread.Sleep(1000);
SPFile page = web.RootFolder.Files["default.aspx"];
SPLimitedWebPartManager manager = page.GetLimitedWebPartManager(PersonalizationScope.Shared));
foreach (Microsoft.SharePoint.WebPartPages.WebPart webPart in webParts)
{
if (webPart.Title == "onetodelete")
{
manager.DeleteWebPart(webPart);
}
}
page.Update();
}