Creating content items
To create a content item you can use the Native API or the Fluent API.
When creating a content item, you must perform the following:
-
Create new content item.
First, you must create new content item.
-
Set the required properties.
When creating a new content item, it is recommended to set at least the following properties:
-
Title
-
Name
-
Content
-
UrlName
-
DateCreated
-
LastModified
You can also set any other properties (e.g. Author, etc.) in this step.
-
Publish the content item.
After you set all properties, you must publish the content item.
-
Save the changes.
Finally, you save all changes that you have made to the content item.
To create a content item using the Native API, you must use the PageManager class. For more information, see Creating a content item using Native API.
To create a content item using the Fluent API, you must use the content item facade. For more information, see Creating a content item using Fluent API.
NOTE: In the code examples below the ID argument is assigned to the master version of the content item.
Creating a content item using Native API
The following code creates a content item with the specified ID, Title, Name, Content and Author through the Native API.
public void CreateContentItemNative(Guid contentItemId, string title, string name, string content, string author)
{
ContentManager manager = ContentManager.GetManager();
ContentItem item = manager.CreateContent(contentItemId);
item.Title = title;
item.Name = name;
item.Content = content;
item.UrlName = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-"); ;
item.DateCreated = DateTime.Now;
item.LastModified = DateTime.Now;
item.Author = author;
manager.Publish(item);
manager.SaveChanges();
}
First, you initialize the ContentManager. You create the content item using the CreateContent method. Then, you set the properties of the content item. It is recommended to set at least the following properties: Title, Name, Content, UrlName, DateCreated and LastModified. To publish the page, you use the Publish method of the manager. Finally, you save the changes.
Creating a content item using Fluent API
The following code creates a content item with the specified ID, Title, Name, Content and Author through the Fluent API.
public void CreateContentItemFluent(Guid contentItemId, string title, string name, string content, string author)
{
App.WorkWith().ContentItem().CreateNew(contentItemId).Do(cI =>
{
cI.Title = title;
cI.Name = name;
cI.Content = content;
cI.UrlName = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
cI.DateCreated = DateTime.Now;
cI.LastModified = DateTime.Now;
cI.Author = author;
})
.Publish()
.SaveChanges();
}
First, you initialize the content item facade by calling App.WorkWith().ContentItem(). You create the content item using the CreateNew method of the facade. Then, you set the properties of the content item. It is recommended to set at least the following properties: Title, Name, Content, UrlName, DateCreated and LastModified. To publish the page, you use the Publish method of the facade. Finally, you save the changes.
See Also