Create and publish content

How to create a folder, content record, and add an iPart.

using System;
using Asi.Business.Common;
using Asi.Business.ContentManagement;

BusinessContainer container = new BusinessContainer("AddExamplesContainer");
// Get the HierarchyKey of the folder to store the content.
// Check to see if the full path we want exists.
string path = "@/iCore/Archive/";
// Adding a "Examples" folder.
Guid hkey = DocumentSystem.HierarchyKeyByPath(path + "Examples");
if (hkey.Equals(Guid.Empty))
{
  // Folder doesn't exist, add it to the pre-existing root folder
  hkey = DocumentSystem.HierarchyKeyByPath(path);
  if (hkey.Equals(Guid.Empty))
    throw new Exception("Unable to create content: the path " + path + " does not exist.");

  ContentFolder folder = new ContentFolder(hkey, HierarchyAddType.AsLastChild, container);
  folder.Name = folder.Title = "Examples";
  folder.Save();
  hkey = folder.HierarchyKey;
}

// Create the content record
Content content =
  new Content(hkey, HierarchyAddType.AsLastChild, container)
{
  ContentTitle = "Create content example " +
    DateTime.Now.ToString("mmssfff", System.Globalization.CultureInfo.InvariantCulture),
  ContentName =
    "Example_" +
    DateTime.Now.ToString("mmssfff", System.Globalization.CultureInfo.InvariantCulture),
  Keywords = "Content, Example",
  ContentDescription = "Content created from a code example"
};

// Create the Content HTML iPart
ContentHtml contentHtml = new ContentHtml(content.ContentKey)
{
  ContentItemName = "ExampleContent",
  Body = "This is example content text"
};

// Add Content HTML iPart to the content record
content.Items.Add(contentHtml);

// Save
content.Save();
container.CommitAll();

// Publish the content record.
content.Publish();
container.CommitAll();