PublishingPage.Layout property

Gets and sets the PageLayout for this PublishingPage object.

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

public PageLayout Layout { get; set; }

Property value

Type: Microsoft.SharePoint.Publishing.PageLayout
The PageLayout for this PublishingPage object.

ExceptionCondition
[System.ArgumentException]

PageLayout.AssociatedContentType does not match the PublishingPage.ContentType

[System.UnauthorizedAccessException]

The current user does not have sufficient permissions to perform this action.

[System.ArgumentNullException]

Indicates that the value being set was a null reference (Nothing in Visual Basic), which is not valid.

The Layout property provides rendering information for the PublishingPage, and matches the ContentType. It supports rendering the content defined by the SPContentType object.

The PublishingPage.Layout value is initialized when a PublishingPage is created (see [M:Microsoft.SharePoint.Publishing.PublishingPageCollection.Add(System.String,Microsoft.SharePoint.Publishing.PageLayout]). Unlike the PublishingPage.ContentType object, you can modify the PublishingPage.Layout after creation. Only PageLayouts that are associated with the current PublishingPage.ContentType are valid. If you attempt to set a layout that is not valid, the system throws a System.ArgumentException exception.

The PageLayout generally should be a member of the PageLayoutCollection that is returned by the Publishing.GetAvailablePageLayouts(SPContentType) method, but SharePoint 2010 does not enforce this.

The PageLayout may return a null reference (Nothing in Visual Basic) if the PublishingPage is disconnected from its PageLayout object.

To save changes after setting this property, you must call the Update method.

To set this value, the user must have both View and Edit permissions on the PublishingPage, View permissions to retrieve the page and to return its property values, and Edit permissions to change the value.

This example replaces the PageLayout object for all PublishingPage objects in a Web to a new PageLayout that renders the same type of content.

Before compiling and running this example, verify that the oldPageLayout and newPageLayout render the same content. They should use the same AssociatedContentType value.

using PublishingPage = Microsoft.SharePoint.Publishing.PublishingPage;
using SPListItem = Microsoft.SharePoint.SPListItem;
using SPFile = Microsoft.SharePoint.SPFile;
using SPModerationStatusType = Microsoft.SharePoint.SPModerationStatusType;
using PublishingWeb = Microsoft.SharePoint.Publishing.PublishingWeb;
using SPUser = Microsoft.SharePoint.SPUser;
using PageLayout = Microsoft.SharePoint.Publishing.PageLayout;
using PublishingPageCollection = Microsoft.SharePoint.Publishing.PublishingPageCollection;

namespace Microsoft.SDK.SharePointServer.Samples
{
    public static class PublishingPageCodeSamples
    {
        public static void SwapPageLayout(PublishingWeb publishingWeb, PageLayout oldPageLayout, PageLayout newPageLayout)
        {
            // Replace these variable values and input parameters
            // with your own values.
            //
            // The comment to set when the page is checked in, published, and
            // approved.
            string checkInComment = "Your comments";
            //
            // Validate the input parameters.
            if (null == publishingWeb)
            {
                throw new System.ArgumentNullException("publishingWeb");
            }
            if (null == oldPageLayout)
            {
                throw new System.ArgumentNullException("oldPageLayout");
            }
            if (null == newPageLayout)
            {
                throw new System.ArgumentNullException("newPageLayout");
            }
            // Confirm that the oldPageLayout and newPageLayout are compatible.
            if (oldPageLayout.AssociatedContentType.Id != newPageLayout.AssociatedContentType.Id)
            {
                throw new System.ArgumentException(
                    "The page layouts must render the same type of content",
                    "newPageLayout");
            }

            System.Guid oldPageLayoutId = oldPageLayout.ListItem.File.UniqueId;

            // Set the new PageLayout for all pages that use the old PageLayout.
            PublishingPageCollection publishingPages = publishingWeb.GetPublishingPages();
            foreach (PublishingPage publishingPage in publishingPages)
            {
                if (publishingPage.Layout.ListItem.UniqueId == oldPageLayoutId)
                {
                    if (publishingPage.ListItem.File.CheckOutStatus == SPFile.SPCheckOutStatus.None)
                    {
                        publishingPage.CheckOut();
                    }

                    publishingPage.Layout = newPageLayout;
                    publishingPage.Update();

                    // The PublishingPage has the same SPContentType as its PageLayout.
                    System.Diagnostics.Debug.Assert(
                        publishingPage.ContentType.Parent.Id ==
                        newPageLayout.AssociatedContentType.Id);

                    publishingPage.CheckIn(checkInComment);
                }
            }

        }

    }
}
Show: