# Hide web part that is defined in Site Definition

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.

<Modules>
<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)
{
if(webpart.Title=="Documents")
{
toDelete = webpart;
}
}
if (toDelete != null)
wps.DeleteWebPart(toDelete);
}
}


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.

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

e.g.

foreach(System.Web.UI.WebControls.WebParts webpart in wps.WebParts)
{
Console.WriteLine(webpart.Title);
}


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

try{