SharePoint 2007 API – How To Change Layout and Publish Page

The hardest part about this (by far) is figuring out how to publish the page after you’ve made your changes.   Every time I need to do it, I have to dig up some old code.

Thought I’d blog it this time, to make it easier on myself next time around.

using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
...

using (SPSite site = new SPSite("http://portal"))
{
    SPWeb web = site.RootWeb;

    // Check to ensure the web has publishing turned on
    if (!PublishingWeb.IsPublishingWeb(web))
    {
        throw new ApplicationException("Web does not have publishing enabled");
    }

    // Get a reference to the publishing web and publishing site    
    PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
    PublishingSite pubSite = new PublishingSite(web.Site);

    // Get page layout from the page layouts collection    
    PageLayoutCollection siteLayouts = pubSite.GetPageLayouts(false);
    PageLayout myLayout = siteLayouts["/_catalogs/masterpage/MyLayout.aspx"];

    // Get a reference to a publishing page
    PublishingPageCollection pages = pubWeb.GetPublishingPages();
    PublishingPage page = pages["pages/default.aspx"];
    
    // Check out the list item as needed
    bool forceCheckout = page.ListItem.ParentList.ForceCheckout;
    if (forceCheckout)
    {
        // Is the page checked out?
        if (page.ListItem.File.CheckedOutBy != null)
        {
            // Throw an exception if the page is checked out
            string pageCheckedOut = string.Format("Page {0} is checked out by {1}", page.Url, page.ListItem.File.CheckedOutBy);
            throw new SPException(pageCheckedOut);
        }

        // Check out the page
        page.CheckOut();
    }

    // Change the page layout
    page.Layout= myLayout;

    // Update the page and check in changes
    page.Update();

    // Publish the page
    // This handles the page checkin and publishing the draft
    ApprovePublishingPage(page, "Modified page layout");

}

/// <summary>
/// Approves changes to a publishing page
/// </summary>
/// <param name="page">PublishingPage with changes to be published</param>
/// <param name="comment">Comment associated with the change</param>
public void ApprovePublishingPage(PublishingPage page, string comment)
{
    // Check in the page if required
    SPFile pageFile = page.ListItem.File;
    if (pageFile.Level == SPFileLevel.Checkout)
    {
        pageFile.CheckIn(comment, SPCheckinType.MajorCheckIn);
    }

    // Skip these steps if the parent list doesn't support moderation
    if (page.ListItem.ParentList.EnableModeration)
    {
        // If page is in "pending" status, approve it
        SPModerationInformation modInfo = page.ListItem.ModerationInformation;
        if (modInfo.Status == SPModerationStatusType.Pending)
        {
            pageFile.Approve(comment);
        }

        // If page is in draft status, publish it
        if (modInfo.Status == SPModerationStatusType.Draft)
        {
            pageFile.Publish(comment);
        }
    }
}

Enjoy!

  del.icio.us it! digg it! reddit! technorati! yahoo!

2 responses to “SharePoint 2007 API – How To Change Layout and Publish Page

  1. where is “page” coming from here? i dont see it instantiated

Leave a comment