Update Master Page Gallery in Publishing site
August 20, 2009
There are times when you have a requirement of only selecting “Custom Page layouts” when a user wants to create a new page in Sharepoint Portal site.
I won’t go in details of how to adding and deploying new custom page layouts using feature, but you can refer here for it :
http://blogs.msdn.com/syedi/archive/2008/08/01/custom-site-definition-for-collaboration-portal-template-sharepoint-2007-wow-moss.aspx
http://spreflections.wordpress.com/2008/09/24/deploy-master-page-and-page-layout-as-a-feature/
Once you have a added a custom page layout, you don’t want the user to pick from any sharepoint built-in page layouts.
To achieve that functionality you can mark those layout types to be hidden on the feature activation code. As all the page layouts are item in a list which is source controlled via sharepoint you will need to check out the file and then only update the “Hidden Page” attribute.
here is how you can ahcieve it.
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["Master Page Gallery"];
foreach (SPListItem item in list.Items)
{
//Note that if your page layout's content type needs to be different
if (item["Associated Content Type"] != null && (item["Associated Content Type"].ToString().IndexOf("Article Page") != -1
|| item["Associated Content Type"].ToString().IndexOf("Welcome") != -1))
{
item.File.CheckOut();
item["Hidden Page"] = "True";
item.Update();
item.File.CheckIn("");
}
}
list.Update();
web.Update();
}
now when the feature is activated, user will only be able to see custom page layouts.
Good luck!