I am trying to remove some WebParts from a page in my site collection. Normally I can do this with SPLimitedWebPartManager.DeleteWebPart(webPart) however, that will not delete System.Web.UI.WebControls.WebParts.Part nor can I cast it to a WebPart object (it returns null).

Currently I am just setting visible attribute to false, but ideally I would like to delete it.

Any suggestions?

  • Are you able to remove the webpart using ?contents=1 query string? – Andrey Markeev Feb 3 '12 at 21:06
  • I am not 100% sure what you mean by that, but I tried your post below and it worked. – Meyer Denney Feb 3 '12 at 21:21
  • 1
    If you append ?contents=1 to a portal page url (i.e. http://site/default.aspx?contents=1), the Maintenance page will be shown, and you'll be able to delete webparts through GUI. Although the problem is solved, this is a quite useful notion anyway :) – Andrey Markeev Feb 3 '12 at 21:38
  • 1
    System.Web.UI.WebControls.WebParts.Part is an abstract class and the base class of all webparts, you can't have an actual webpart on a page with that object type. Or did you mean "webparts derived from that class"? I'm confused. – Louis Feb 3 '12 at 22:53

You should be able to get the WebPart object by indexer of the WebParts collection, and then pass it to DeleteWebPart method without any additional casting.

For example:


The indexer returns strongly-typed System.Web.UI.WebControls.WebParts.WebPart object, at least as it goes from the MSDN documentation, so this should work (however, didn't test it myself).

  • I tried your idea and it worked. – Meyer Denney Feb 3 '12 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.