Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

1 Answer

up vote 4 down vote accepted

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:

manager.DeleteWebPart(manager.WebParts[7]);

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).

share|improve this answer
    
I tried your idea and it worked. –  Meyer Denney Feb 3 '12 at 21:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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