Limiting SharePoint available PageLayout to create new pages from

When creating new intranet or internet based on SharePoint you probably have lots of sections and topic areas. You probably also have lots of page layouts that you provision to facilitate various functions on your site. The problem comes when your users try to create a new page on one of the areas, say News, and they get all of the other irrelevant page layouts for example layout for an announcement.

To avoid this situation you can use AvailablePageLayouts property of a PublishingWeb to specify layouts that are applicable to this section and optionally say whether underlying webs are going to inherit the same pattern or not.

Here is a feature that will accept 2 parameters which will define the file name of the layouts template and whether underlying webs will inherit it. You can place this feature in your ONET.XML or into your site provisioning script and define each sites properties automatically through deployment.

Here is my Feature.XML

<?xml version=”1.0″ encoding=”utf-8″?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/”
 Id=”23812384-EAE5-4780-AA85-44F187D1D915″
 Title=”Set allowed layout collection”
 Description=”Set allowed layout collection”
Version=”1.0.0.0″
Scope=”Web”
Hidden=”False”
ActivateOnDefault=”FALSE”
AlwaysForceInstall=”TRUE”
ReceiverAssembly=”MyProject.Features, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0bcd2aaa03ff3d79″
ReceiverClass=”MyProject.Features.SetAllowedLayouts”>
<ElementManifests>
</ElementManifests>
</Feature>

<?xml version=”1.0″ encoding=”utf-8″?>

<Feature xmlns=”http://schemas.microsoft.com/sharepoint/”

Id=”23812384-EAE5-4780-AA85-44F187D1D915″

Title=”Set allowed layout collection”

Description=”Set allowed layout collection”

Version=”1.0.0.0″

Scope=”Web”

Hidden=”False”

ActivateOnDefault=”FALSE”

AlwaysForceInstall=”TRUE”

ReceiverAssembly=”MyProject.Features, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0bcd2aaa03ff3d79″

ReceiverClass=”MyProject.Features.SetAllowedLayouts”>

<ElementManifests>

</ElementManifests>

</Feature>

And here is how my FeatureActivated looks like:
 
using (SPWeb web = properties.Feature.Parent as SPWeb)
            {
                PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
                string allowedLayouts = string.Empty;
                string[] allowedLayoutArray = null;
                bool subsitesInherit = false;
                if (properties.Feature.Properties["AvailablePageLayouts"] != null)
                {
                    allowedLayouts = properties.Feature.Properties["AvailablePageLayouts"].Value;
                    allowedLayoutArray = allowedLayouts.Split(‘;’);
                }
                if (properties.Feature.Properties["ResetAllSubsitesToInherit"] != null)
                {
                    subsitesInherit =
                        properties.Feature.Properties["ResetAllSubsitesToInherit"].Value.ToLower().Equals(“true”);
                }
                PageLayout pageLayout = null;
                PageLayout[] pageLayouts = new PageLayout[allowedLayoutArray.Length];
                SPList masterPageList = publishingWeb.GetAvailablePageLayouts()[0].ListItem.ParentList;
                if (masterPageList != null)
                {
                    for (int i = 0; i < allowedLayoutArray.Length; i++)
                    {
                        for (int j = 0; j < masterPageList.Items.Count; j++)
                        {
                            if (masterPageList.Items[j]["Name"].ToString().Equals(allowedLayoutArray[i]))
                            {
                                pageLayout = new PageLayout(masterPageList.Items[j]);
                                pageLayouts[i] = pageLayout;
                            }
                        }
                    }
                    publishingWeb.SetAvailablePageLayouts(pageLayouts, subsitesInherit);
                    publishingWeb.Update();
                }
            }
Enjoy!
This entry was posted in MOSS, sharepoint and tagged , , , . Bookmark the permalink.

Comments are closed.