Umbraco/Samples and Articles/Creating umbraco pages programmatically

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Creating umbraco pages programmatically[edit]

This is based on an original article at

Getting Started[edit]

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 


// Tell the runtime environment to publish this document 


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.