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 not valid.

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)
  {
// 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);
}

  }

    }
}
Imports SPSite = Microsoft.SharePoint.SPSite
Imports PublishingSite = Microsoft.SharePoint.Publishing.PublishingSite
Imports SPWeb = Microsoft.SharePoint.SPWeb
Imports PublishingWeb = Microsoft.SharePoint.Publishing.PublishingWeb
Imports PublishingWebCollection = Microsoft.SharePoint.Publishing.PublishingWebCollection
Imports SPWebTemplate = Microsoft.SharePoint.SPWebTemplate

Namespace Microsoft.SDK.SharePointServer.Samples
    Public NotInheritable Class PublishingWebCollectionCodeSamples

  ' This method creates a new PublishingWeb below the root Web site of a PublishingSite object.
  '
  ' Prerequisites:
  ' The SPSite passed should be a site that supports publishing.
  '
  ' This sample demonstrates use of:
  ' PublishingSite.IsPublishingSite( SPSite )
  ' PublishingSite constructor
  ' PublishingSite.RootWeb
  ' PublishingWeb.GetPublishingWebs()
  ' PublishingWeb.Web
  ' PublishingWebCollection.Add(string, uint, string)
  Private Sub New()
  End Sub
  Public Shared Sub CreatePublishingWebBelowRoot(ByVal site As SPSite, ByVal webTemplate As SPWebTemplate)
' Replace these variable values and input parameters with your own values.
' yourWebURL: name for the PublishingWeb object to create.
Dim yourWebUrlName As String = "yourWebUrl"

Dim newWeb As PublishingWeb = Nothing

Try
    '
    ' Validate the input parameters.
    '
    If Nothing Is site Then
  Throw New System.ArgumentNullException("site")
    End If
    If Nothing Is webTemplate Then
  Throw New System.ArgumentNullException("webTemplate")
    End If

    Dim publishingSite As PublishingSite = Nothing
    If Not PublishingSite.IsPublishingSite(site) Then
  Throw New System.ArgumentException("The SPSite is expected to be a PublishingSite", "site")
    End If
    publishingSite = New PublishingSite(site)

    Dim rootWeb As SPWeb = publishingSite.RootWeb
    If Not PublishingWeb.IsPublishingWeb(rootWeb) Then
  Throw New System.ArgumentException("The SPSite.RootWeb is expected to be a PublishingWeb", "site")
    End If

    Dim rootPublishingWeb As PublishingWeb = PublishingWeb.GetPublishingWeb(rootWeb)
    Dim publishingWebs As PublishingWebCollection = rootPublishingWeb.GetPublishingWebs()

    '
    ' Create the new PublishingWeb object by using the sample values provided.
    '
    newWeb = publishingWebs.Add(yourWebUrlName, rootWeb.Language, webTemplate.Name) ' Replace with your Web template name. -  Replace with your own language value.

    ' The new PublishingWeb has the Publishing feature active.
    System.Diagnostics.Debug.Assert(Nothing IsNot newWeb.Web.Features(Microsoft.SharePoint.Publishing.FeatureIds.Publishing))

Finally
    '
    ' Always close the SPWeb object when done to release memory.
    '
    If Nothing IsNot newWeb Then
  newWeb.Web.Close()
    End If
End Try

  End Sub
    End Class
End Namespace

See Also

Reference

PublishingSite Class

PublishingSite Members

GetPageLayouts Overload

Microsoft.SharePoint.Publishing Namespace

PublishingSite

GetPageLayouts

PageLayouts

GetAvailablePageLayouts

GetAvailablePageLayouts