Computer Magic Logo
Content creation

Friday, November 20, 2015

Published by Aristotelis Pitaridis

In order to create a content we use the Umbraco Content Service. We have to define the Content name which will be displayed in the contents tree. We also have to define the id of the parent content. finally we have to define the alias of the Document Type which will be used in order to create our content. Optionally we can set the value of properties defined in the document type. We use the SaveAndPublishWithStatus member function in order to save our content.

// Get the Umbraco Content Service
var contentService = ApplicationContext.Current.Services.ContentService;

// Set the required information
string ContentName = "My content name";
int ParentID = CurrentPage.Id;
string DocumentTypeAlias = "products";

// Create the content
var MyContent = contentService.CreateContent(ContentName, ParentID, DocumentTypeAlias);

// Set the property values
MyContent.SetValue("Title", "This is the title");

// Save and publish the content
contentService.SaveAndPublishWithStatus(MyContent);