1

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.

2
  • 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, 2011 at 9:19
  • The code is added.
    – oglester
    Jul 5, 2011 at 13:38

2 Answers 2

1

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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