Wednesday, September 7, 2011

Swap page layout for page,sharepoint 2010

This exercise demonstrates how to change the pagelayout with the new pagelayout.



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

        }

No comments:

Post a Comment

Popular Posts