Search code examples
c#sharepoint-2007web-partsmoss

SharePoint 2007 - Update all site home pages


I am wondering if there is a way to update all site home pages to display a new custom web part.

I have a site collection with 100+ sub sites and I have created a custom web part which i want to display in all site home pages.. what would you suggest as the best way to do this, as as doing this manually will take considerable time?!


Solution

  • First write the code to programmatically add a webpart to a single homepage. The specifics of how to do this will vary based on how your homepage is structured, whether it's a publishing page, etc. It's most likely possible, but implementations could differ. You'll possibly be using something similar to this:

    using(SPSite site = new SPSite("http://localhost"))
    using(SPWeb web = site.RootWeb)
    {
        web.AllowUnsafeUpdates = true;
        SPLimitedWebPartManager webParts = web.GetLimitedWebPartManager("Pagees/Home.aspx"
            , System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
    
        MyWebPart wp = new MyWebPart();      // your custom webpart
        wp.Title = "My WebPart";
    
        webParts.AddWebPart(wp, "Left", 0);
        webParts.SaveChanges(wp);
    }
    

    There are lots of variations when searching the subject online.

    Once you have that you can create either a console application or a feature to be executed on the top level site, open up each subsite, and then execute the above code.