SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to hide or remove a Web Part, a View of a document library, from Webs that have been created using a Site Definition that specifies this View.

In ONET.XML, the following is what sets up the Web Part view.

    <Module Name="Default" Url="" Path="">
        <File Url="default.aspx" NavBarHome="True" Type="Ghostable">
            <View List="Documents" BaseViewID="0" WebPartZoneID="Left" WebPartOrder="3" />

When I try and access it via the LimitedWebPartManager, I don't see the Web Part.

Here is the code

        using (var web = GetWeb("/path"))
            Microsoft.SharePoint.WebPartPages.WebPart toDelete = null;
            using (var wps = web.GetLimitedWebPartManager(
                    "default.aspx", PersonalizationScope.Shared))

                foreach (Microsoft.SharePoint.WebPartPages.WebPart 
                          webpart in wps.WebParts)
                        toDelete = webpart;
                if (toDelete != null)

More information:

The web parts are coming back as Microsoft.SharePoint.WebPartPages.ErrorWebPart because it is running from a utility rather than a feature or web part.

share|improve this question
Please post the code that you are using. You should be able to enumerate them (all other things being well). Do you get any error messages? Are you sure you have the correct permissions? – SHug Jul 4 '11 at 9:19
The code is added. – oglester Jul 5 '11 at 13:38

You may be running into problems iterating as you're using the Microsoft.SharePoint.WebPartPages.WebPart class - not every web part in SharePoint is a SharePoint web part. Try using the more general System.Web.UI.WebControls.WebParts class.


foreach(System.Web.UI.WebControls.WebParts webpart in wps.WebParts)

Try it in a console app - it tends to be easier to see problems without all the other layers involved.

Also, don't forget that SPLimitedWebPartManager leaks a web, so you should finish off with...

    ... Your code ...

As from here

share|improve this answer
up vote 0 down vote accepted

So, my solution is to simply move the code to a feature and activate it to accomplish my goal.

share|improve this answer

Your Answer


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.