HOME ABOUT
I AM HERE
  • Twitter icon
  • Facebook icon
  • Technorati icon
Bookmark and Share

Adding web pages programatically to MOSS 2007

August 10, 2007 21:48 by Aidan

I have a requirement to add several pages to a WCM enabled MOSS 2007 site programatically. To do this I needed to add page items to the pages list in the WCM site. To access the pages list you need to first get hold of the relevant site and cast it to a PublishingWeb. Once that has been done the content type is found and the page layout is chosen. At that point the page can be created, have values assigned to it and even be published and approved programatically.

The code to do this is:   

            //get site to add page to

            SPSite Site = new SPSite(siteName);
            SPWeb Web = Site.OpenWeb();

            //cast it to a publishing site

            PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(Web)                           

            //get Page content type - open the relevant content type to get it's GUID
                SPContentTypeId ContentTypeID = new SPContentTypeId("0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3

DB064584E219954237AF39005A25699F214C32448043AC003089FFB0");

                //get page layout
                PageLayout[] layouts = publishingWeb.GetAvailablePageLayouts(ContentTypeID);
                PageLayout pageLayout = layouts[0];

                //create a new page
                PublishingPage newPage = publishingWeb.GetPublishingPages().Add("newPageName" + ".aspx", pageLayout);
                               
                newPage.ListItem["columnName"] = "Some Value"

                SPFile listItemFile = page.ListItem.File;

                //check that the file is not checked out - if it is check it in. This is more important when you have been modifying existing pages

                if (listItemFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                {
                    listItemFile.CheckIn("");
                }
                listItemFile.Publish("");
                listItemFile.Approve("");

                Thread.Sleep(1000);
                               
                Web.Dispose();
                Site.RootWeb.Dispose();
                Site.Dispose();

Thread sleep is there in case you are adding many pages at once as this causes problems for the variations job and only the first page added is propagated. By adding a sleep the pages are added more slowly and the variation job works as expected!


Tags:
Categories: MOSS 2007
Actions: E-mail | Permalink | Comments (3) | Comment RSSRSS comment feed

Comments

June 18. 2008 20:58

Thanks for the information. I got SPexception in this line "PublishingPage newPage = publishingWeb.GetPublishingPages().Add("newPageName" + ".aspx", pageLayout);
" and the error message is "".
P.S. I created a Windows Form and called this code in button clike event. Should I create credential or do something else?

Thank you,

Bill

June 19. 2008 20:13

Bill - what is the exception message?

aidan

July 29. 2008 11:14

Regarding Bill's exception message: I received the following exception at the same line - error 'No Parameterless constructor defined'

I had several web part zones with a default web part declared (similar to the following example code):

<WebPartPages:WebPartZone id="...">
<ZoneTemplate>
<MyWebPart id="" runat="server"...>
</ZoneTemplate></WebPartPages:WebPartZone>

Removing the default MyWebPart from the code fixed the problem.  Presumably the problem is in the initialisation of the web part which has no parameterless constructor. My only work around was to leave the web part zones empty and manually add the web parts to the pages.

Dave Mawer

Add comment




  Country flag


  • Comment
  • Preview
Loading