I’ve been meaning to write about this for a while – how to programmatically disable page layouts for a site in SharePoint, similar to how you’d do so with the ‘Page Layouts and Site Templates’ page in the site settings. You can also specify what page layouts are allowed, if you are defining your own Site Definition. But someday, maybe you’ll have to do it through code.
I’m pretty sure that I read something that set me going on this, but I’ve lost the link 😦
In this example, I’m disabling “Welcome Page” page layouts (I hate those feckers!)
SPSite site = new SPSite("http://vm-moss2007/");
SPWeb web = site.OpenWeb();
PublishingWeb pweb = PublishingWeb.GetPublishingWeb(web);
PageLayout oldPls = pweb.GetAvailablePageLayouts();
ArrayList newPageLayoutList = new ArrayList();
foreach (PageLayout pl in oldPls)
if (pl.AssociatedContentType.Name != "Welcome Page")
PageLayout newPls = (PageLayout)newPageLayoutList.ToArray(typeof(PageLayout));
foreach (PageLayout p in newPls)
Okay, so we start with creating our SPWeb, and PublishingWeb objects. We get the available Page Layouts, which we loop over. If the Page Layout’s Content Type’s name is not “Welcome Page” we add it to a list of page layouts we want available. When we’re done, we set the available page layouts to that array list, and update our site.
Finally, we close some of the objects we’ve opened.