Migrating the Publishing Pages for Fun and Profit

This week and the following weeks I will be working through a ton of recursive functions to move a rather hefty page library into a Site Collection and subwebs.  I figure while I am having some “fun” you guys will profit will a few of my scripts.  For any of you out there who are interested in how to create a publishing page in a library here is a little sample code on how to copy items from the page content area and create a new page then replace a welcome page.  Here is a small sample:

PublishingSite PubSite = new PublishingSite(NewSite);  //pass in an SPSite Object
PageLayoutCollection SiteLayouts = PubSite.GetPageLayouts(false);
PageLayout MyLayout = SiteLayous[“urlforlayout”];
PublishingWeb PubWeb = PublishingWeb.GetPublishingWeb(Web); //pass in an SPWeb Object
PublishingPageCollection Pages = PubWeb.GetPublishingPages();
PublishingPage MyPage = PubWeb.GetPublishingPages().Add(ChildItem["Name"].ToString(), MyLayout);
MyPage.ListItem["Page Content"] = ChildItem["Page Content"];
MyPage.ListItem.Update();
MyPage.Update();
MyPage.CheckIn("New Page Creation");
PubWeb.DefaultPage = Web.GetFile(MyPage.Url);
PubWeb.Update();

 

So basically if you look at the top you get the Publishing Site and then you get the Publishing Web.  What you want to do next is get one of the layout pages.  You could instead of hard-coding in a URL step through a foreach loop and search for the specific layout that you want.  What you want to do next is create a new page and then give it the name of the old item, so it will create the item with the same page name and you want to pass in the layout page from the previous step.  Next you want to get the Page Content from the Content Area in the “Page Content” field and then update the page.  Remember you need to check the item in before you set the welcome page or you will receive a fun little error message.  Then you can set the Publishing Web’s default page (welcome page) to your page and update the Publishing Web.  I learned the hard way by not updating the Publishing Web the first few times.  You also need to remember that you should turn on the flag for Web.AllowUnsafeUpdates otherwise you will not be able to create a page or update anything at all in code.  After you are done turn this flag off as a good practice.  Remember to dispose all SPWeb and SPSite object if necessary or wrap using statements around everything.  I hope you enjoy!

Print | posted on Saturday, February 6, 2010 3:58 PM

Feedback

# re: Migrating the Publishing Pages for Fun and Profit

Left by Aidan Garnish at 2/8/2010 4:48 AM
Gravatar Here are a few more scripts and a web part that do some similar web content managementy type things. Hope someone finds them useful - http://www.aidangarnish.net/post/Random-SharePoint-utilities.aspx

Your comment:





 
 

Copyright © MOSSLover

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski