Share via


PublishingSite.GetPageLayouts Method (SPContentType, Boolean)

Gets a collection of PageLayout objects from the Master Page Gallery of the RootWeb() property that has a AssociatedContentType property that matches a specific SPContentType object.

Namespace:  Microsoft.SharePoint.Publishing
Assembly:  Microsoft.SharePoint.Publishing (in Microsoft.SharePoint.Publishing.dll)

Syntax

'Declaration
Public Function GetPageLayouts ( _
    associatedContentType As SPContentType, _
    excludeObsolete As Boolean _
) As PageLayoutCollection
'Usage
Dim instance As PublishingSite
Dim associatedContentType As SPContentType
Dim excludeObsolete As Boolean
Dim returnValue As PageLayoutCollection

returnValue = instance.GetPageLayouts(associatedContentType, _
    excludeObsolete)
public PageLayoutCollection GetPageLayouts(
    SPContentType associatedContentType,
    bool excludeObsolete
)

Parameters

  • associatedContentType
    Type: Microsoft.SharePoint.SPContentType
    SPContentType object to match against the AssociatedContentType property values of the PageLayout objects returned in the collection.
  • excludeObsolete
    Type: System.Boolean
    Boolean flag that indicates whether to exclude PageLayout objects that are marked as hidden.

Return Value

Type: Microsoft.SharePoint.Publishing.PageLayoutCollection
A collection of PageLayout objects from the Master Page Gallery of the RootWeb property that have an AssociatedContentType property that matches a specific SPContentType object.

Exceptions

Exception Condition
ArgumentNullException

Indicates that the associatedContentType parameter is a null reference (Nothing in Visual Basic), which is invalid.

Remarks

The associatedContentType parameter must not be a null reference (Nothing in Visual Basic). If the excludeObsolete parameter is set to True, obsolete PageLayout objects that have been marked as hidden are not returned in the collection. If the excludeObsolete parameter is set to False, obsolete PageLayout objects are included in the collection.

Examples

using SPContentTypeId = Microsoft.SharePoint.SPContentTypeId;
using SPContentType = Microsoft.SharePoint.SPContentType;
using SPSite = Microsoft.SharePoint.SPSite;
using SPFile = Microsoft.SharePoint.SPFile;
using SPWeb = Microsoft.SharePoint.SPWeb;
using PublishingSite = Microsoft.SharePoint.Publishing.PublishingSite;
using PublishingWeb = Microsoft.SharePoint.Publishing.PublishingWeb;
using PageLayoutCollection = Microsoft.SharePoint.Publishing.PageLayoutCollection;
using PageLayout = Microsoft.SharePoint.Publishing.PageLayout;

namespace Microsoft.SDK.SharePointServer.Samples
{
    public static class PublishingWebCodeSamples
    {
// This sample restricts the set of available page layouts used
// for creating pages in a publishing Web so that only page layouts
// associated with a certain content type are available.
        //
        // Prerequisites:
        // The associatedContentTypeId parameter is from a content
        // type on the root Web site of the site.
        //
        public static void RestrictPageLayoutsByContentType(
            PublishingWeb publishingWeb, 
            SPContentTypeId associatedContentTypeId)
        {
            // TODO: Replace these variable values and input
            // parameters with your own values.
            bool excludeHiddenLayouts = true;
            bool resetAllSubsitesToInherit = true;

            //
            // Validate the input parameters.
            //
            if (null == publishingWeb)
            {
                throw new System.ArgumentNullException("publishingWeb");
            }

            SPSite site = publishingWeb.Web.Site;
            PublishingSite publishingSite = new PublishingSite(site);

            //
            // Retrieve a collection of all page layouts in the site
            // collection that match the content type.
            //
            SPContentType associatedContentType = publishingSite.ContentTypes[associatedContentTypeId];
            if (null == associatedContentType)
            {
                throw new System.ArgumentException(
                    "The SPContentTypeId did not match an SPContentType in the SPSite.RootWeb",
                    "associatedContentTypeId");
            }

            PageLayoutCollection pageLayoutsByContentType = 
                publishingSite.GetPageLayouts(associatedContentType, excludeHiddenLayouts);

            //
            // Update the Web to use these page layouts when
            // creating pages.
            //
            publishingWeb.SetAvailablePageLayouts(
                pageLayoutsByContentType.ToArray(),
                resetAllSubsitesToInherit);

            publishingWeb.Update();

            //
            // Verify the expected results (this is not required,
            // and simply demonstrates the results of calling the
            //  SetAvailablePageLayouts method).
System.Diagnostics.Debug.Assert(!publishingWeb.IsAllowingAllPageLayouts);
            System.Diagnostics.Debug.Assert(!publishingWeb.IsInheritingAvailablePageLayouts);

            PageLayout[] availablePageLayouts = publishingWeb.GetAvailablePageLayouts();
            foreach (PageLayout pageLayout in availablePageLayouts)
            {
                System.Diagnostics.Debug.Assert(
                    pageLayout.AssociatedContentType.Id == associatedContentTypeId);
            }

        }

    }
}

See Also

Reference

PublishingSite Class

PublishingSite Members

GetPageLayouts Overload

Microsoft.SharePoint.Publishing Namespace

PublishingSite

GetPageLayouts

PageLayouts

GetAvailablePageLayouts

GetAvailablePageLayouts