Geeks With Blogs

About Me:
I am a Female SharePoint Developer (a platinum unicorn). I have been working with SharePoint since I attended the Portal University in 2005. I hold a BA in Computer Science from the University of Missouri - Kansas City. I love playing Rockband, organizing user group meetings, working with code, attending events as a speaker or organizer, and having bizarre conversations about geek things with cool people. If you have any comments or questions fill out the contact form and I will try my best to help.

The Guide of a MOSSLover Becky Isserman's Blog

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.CheckIn("New Page Creation");
PubWeb.DefaultPage = Web.GetFile(MyPage.Url);


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!

Posted on Saturday, February 6, 2010 3:58 PM MOSS | Back to top

Comments on this post: Migrating the Publishing Pages for Fun and Profit

# re: Migrating the Publishing Pages for Fun and Profit
Requesting 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 -
Left by Aidan Garnish on Feb 08, 2010 4:48 AM

Your comment:
 (will show your gravatar)

Copyright © MOSSLover | Powered by: