Programmatically disable page layouts

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")
{
newPageLayoutList.Add(pl);
}
}
PageLayout[] newPls = (PageLayout[])newPageLayoutList.ToArray(typeof(PageLayout));
pweb.SetAvailablePageLayouts(newPls, false);
foreach (PageLayout p in newPls)
{
p.ListItem.Web.Close();
}
pweb.Update();
pweb.Close();
web.Close();
site.Close();

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.

Advertisements
Programmatically disable page layouts

3 thoughts on “Programmatically disable page layouts

  1. vijay says:

    Is this applicable only for publishing sites? Can i use a similar approach to suppress the web part page layouts available for a team or blank site??

    Like

    1. Yeah, this is just for Publishing sites. I don’t know about normal Team Sites, but it’s a good question (the out of box pages for team/blank sites are awful)

      Like

  2. MT says:

    Andy,

    I have a huge problem with Welcome Pages – only those defined using the Welcome Page site content type show up in the list of available page layouts to use when creating a new site collection as a subsite collection of the main site. It seems impossible to change to another. Also, how would one set the default “Welcome Page” to be your own standard page layout not based on Welcome Page at all rather than the default? Can you set a normal page and welcome page default separately?

    Thanks in advance. This issue is time sensitive.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s