x First time here? Check out the FAQ
Join us a the Umbraco event of the year - get your Codegarden 14 early bird ticket today

Creating a document

    Sample code to create a document and then publish it.

    Remember to add the cms.dll, businesslogic.dll and umbraco.dll to your project.

    Add references to the following namespaces at the top of your .cs file

    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.web;

    Perform the creation

    //Get the type you would like to use by its alias 
    //and the user who should be the creator of the document
    DocumentType dt = DocumentType.GetByAlias("Textpage");
    User author = User.GetUser(0);

    //create a document with a name, a type, an umbraco user, and the ID of the document's parent page.
    //To create a document at the root of umbraco, use the id -1

    Document doc = Document.MakeNew("My new document", dt, author, 1018);

    //after creating the document, prepare it for publishing

    doc.Publish(author);

    //Tell umbraco to publish the document
    umbraco.library.UpdateDocumentCache(doc.Id);
    //umbraco.library.PublishSingleNode(doc.Id); - obsolete

     

    See this article for setting properties of the document: our.umbraco.org/.../modifying-document-properties