Creating an Extension X3 page programmatically

by Fredrik Karlsson 18. February 2009 22:24

I've been developing an Extension X2 to Extension X3 migrate tool for a few days now, and one of the things I needed to do was to create an Extension X3 page programmatically.
So I went to both the Extension X3 wiki aswell to the SDK to see some example code, but the content there was obsolete. So I thought I would post some up to date example code here. This is tested for EPiServer CMS R2 and Extension X3 3.2.

Example code

// Create the normal EPiPage
PageData myPage = EPiServer.DataFactory.Instance.GetDefaultPageData(PageReference.StartPage, CurrentPage.PageTypeID);
myPage.PageName = "AutoCreated ExtensionPage" + DateTime.Now.ToString();
PageReference currentPageReference = DataFactory.Instance.Save(myPage, SaveAction.Publish, AccessLevel.NoAccess);
// Load the Extension X3 page
ExtensionPageData subPage = ExtensionPageData.Load(currentPageReference);
subPage.PageLink = currentPageReference;
// get the first content are of the page
ContentAreaData contentArea = subPage.GetContentAreas()[0];
// Create new layout function
ContentFunctionData block = ContentFunctionData.Create(currentPageReference, PageType.Load("[ExtensionSys] FiftyFifty").ID, AccessLevel.NoAccess);
// Add the layout function to the area
contentArea.InsertContentFunction(block, 0);
// Create new Article/Content-function
ContentFunctionData textFunction = ContentFunctionData.Create(currentPageReference, PageType.Load("[ExtensionSys] Article").ID, AccessLevel.NoAccess);
// Add value to the two properties
textFunction.Property["Heading"].Value = new PropertyString("My Heading");
textFunction.Property["MainBody"].Value = new PropertyLongString("My long string");
// Save it
textFunction.Save(SaveAction.Publish | SaveAction.ForceCurrentVersion, AccessLevel.NoAccess);
// Add it in the first content area
block.GetContentAreas()[0].InsertContentFunction(textFunction, 0);
// Update the Extension X3 XML
PageDataManager.UpdatePageStruct(subPage.CurrentPage, subPage);
// Save the EPiPage
DataFactory.Instance.Save(subPage.CurrentPage, SaveAction.Publish | SaveAction.ForceCurrentVersion, AccessLevel.NoAccess);

Tags: , ,

Development

blog comments powered by Disqus

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 2.5 Sweden License.


Welcome to the Dropit blog!

Here we, the people that work at Dropit, will write about stuff that interests us. For example web development, especially with .NET and EPiServer - but we'll also talk about other techniques that interest us, marketing on the web, social phenomenons, pop culture, games and software development in general.