Update Master Page Gallery in Publishing site

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!

Advertisement

About Chintan Jajal
.Net Application Architect..

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.