411 REFERENCE · Wikibooks
Umbraco/Samples and Articles/Creating umbraco pages programmatically
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Creating umbraco pages programmatically
This is based on an original article at
http://www.umbraco.org/frontpage/documentation/implementingnetcontrols/creatingumbracopagesprogrammatically.aspx
Getting Started
It's easy to create new pages programmatically using the umbraco api (businesslogic). Just make sure you reference the umbraco.dll (or if running 2.1: cms.dll and businesslogic.dll).
Here's a few lines of sample code that shows how to do it.
using umbraco.cms.businesslogic.web;
// The documenttype that should be used, replace 10 with the id of your documenttype
DocumentType dt = new DocumentType(10);
// The umbraco user that should create the document,
// 0 is the umbraco system user, and always exists
umbraco.BusinessLogic.User u = new umbraco.BusinessLogic.User(0);
// The id of the parent document
int parent = 100;
// Create the document
Document d = Document.MakeNew("My weblog post", dt, u, parent);
// Add values to the generic properties of the document
// (where bodyText is the alias of your property)
d.getProperty("bodyText").Value = "Lorem Ipsum";
// Set the publish status of the document and there by create a new version
d.Publish(u);
// Tell the runtime environment to publish this document
umbraco.library.PublishSingleNode(d.Id);
In umbraco version 2.1, you can use the DocumentType.GetByAlias(string Alias), so you're not dependent on identifiers. This is best practice and very important in order to make your code re-usable. If you're running 2.0, make sure that the id of the documenttype can be changed from umbraco by making a public property.