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 have an event receiver that modifies some web parts when a new blog site is created:

using (SPLimitedWebPartManager wpManager = clientWeb.GetLimitedWebPartManager(SPUtility.ConcatUrls(clientWeb.Url, "default.aspx"), System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
{
    foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in wpManager.WebParts)
    {
        switch (webPart.Title)
        {
            case "About this blog":
                webPart.Hidden = true;
                wpManager.SaveChanges(webPart);
                break;
            case "Links":
                webPart.Hidden = true;
                wpManager.SaveChanges(webPart);
                break;
            case "Blog Tools":
                webPart.Hidden = true;
                wpManager.SaveChanges(webPart);
                break;
            case "Posts":
                webPart.Title = "Blog Posts";
                webPart.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleOnly;
                wpManager.SaveChanges(webPart);
                break;
            default:
                break;
        }
    }
}

This code hides About this blog, Links and changes the name of Posts but I can't get it to hide the Blog Tools web part, when I debug the code the name is Blog Tools so I can find it but why doesn't it get hidden?

Thanks in advance

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

For Blog Tools web part (Blog Admin web part) the Hidden property cannot be set to true explicitly. This property is initialized during OnLoad event and depends on the following conditions:

So, in order to hide this web part another options should be considered. For example, it could be hidden on the client side.

share|improve this answer
add comment

Instead of using SPLimitedWebPartManager you could use the ASP.NET controls collection to find a control of type Microsoft.SharePoint.WebPartPages.BlogAdminWebPart and set Visible = false.

To find the control in question you need to start with this.Page.Controls and do a recursive loop through all controls until you found it (note: this might put quite some execution time on each page load, so you might probably want to apply a smarter way to locate the WebPart control if you can be sure to find it always at the same WebPart zone/place).

Put all that code into a SharePoint AdditionalPlaceHead control and create a feature which staples it to the SPWeb of your choice.

Been there, done that for a couple of years now (even hiding the whole WebPart zone containing the BlogAdminWebPart).

share|improve this answer
    
Thanks for your post, had already marked Vadim's answer though –  Peter Mar 21 '13 at 5:43
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.