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 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);
            }
        }

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

2 Answers 2

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

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{
    ... Your code ...
}
finally
{
    wps.Web.Dispose();
}

As from here

share|improve this answer

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.