versionFrom | verified-against | meta.Title | meta.Description |
---|---|---|---|
9.0.0 |
rc-1 |
Content Model |
The Content class represents a single item in the content tree, its values are fetched directly from the database, not from the cache. |
The Content
class represents a single item in the content tree, its values are fetched directly from the database, not from the cache. Notice the Content class should strictly be used for CRUD operations, not complex queries, as it is not flexible nor fast enough for this.
All content is versioned, so on each individual change, a new version is stored. Past versions can only be retrieved from the Content
api, not from the cache.
- Namespace:
Umbraco.Cms.Core.Models
- Assembly:
Umbraco.Core.dll
All samples in this document will require references to the following dll:
- Umbraco.Core.dll
All samples in this document will require the following using statements:
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
Constructor for creating a new Content object where the necessary parameters are the name of the Content, the parent of the Content as an IContent
object and the ContentType as an IContentType
object for the Content being created. In addition, there is an optional parameter for the culture.
new Content(string name, IContent parent, IContentType contentType, int userId, string culture = null)
Constructor for creating a new Content object where the necessary parameters are the name of the Content, the parent of the Content as an IContent
object, the ContentType as an IContentType
object for the Content being created and the id of the user as a int
. In addition, there is an optional parameter for the culture.
new Content(string name, IContent parent, IContentType contentType, PropertyCollection properties, string culture = null)
Constructor for creating a new Content object where the necessary parameters are the name of the Content, the parent of the Content as an IContent
object, the ContentType as an IContentType
object and a PropertyCollection
for the Content being created. In addition, there is an optional parameter for the culture.
Constructor for creating a new Content object where the necessary parameters are the name of the Content, the id of the parent as int
and the ContentType as an IContentType
object for the Content being created. In addition, there is an optional parameter for the culture.
Constructor for creating a new Content object where the necessary parameters are the name of the Content, the parent id of the Content as an int
object, the ContentType as an IContentType
object for the Content being created and the id of the user as a int
. In addition, there is an optional parameter for the culture.
new Content(string name, int parentId, IContentType contentType, PropertyCollection properties, string culture = null)
Constructor for creating a new Content object where the necessary parameters are the name of the Content, the id of the parent as int
, the ContentType as an IContentType
object and a PropertyCollection
for the Content being created.In addition, there is an optional parameter for the culture.
Gets or Sets a DateTime
object, indicating then the given Content was created.
// Given a `ContentService` object get Content by its Id and return CreateDate
var content = contentService.GetById(1234);
return content.CreateDate;
Gets or Sets the Id of the User
who created the Content.
// Given a `ContentService` object get Content by its Id and return the Id of the Creator
var content = contentService.GetById(1234);
return content.CreatorId;
Returns a ISimpleContentType
object representing the DocumentType used by the given Content
.
// Given a `ContentService` object get Content by its Id and return ContentType
var content = contentService.GetById(1234);
return content.ContentType;
Returns the id as an int
of the ContentType
object representing the DocumentType used by the given Content
.
// Given a `ContentService` object get Content by its Id and return the Id of the ContentType
var content = contentService.GetById(1234);
return content.ContentTypeId;
Returns the ContentScheduleCollection
.
// Given a `ContentService` object get Content by its Id and return the content schedule
var content = contentService.GetById(1234);
return content.ContentSchedule;
Returns the unique Content
Id as a Int
, this ID is based on a Database identity field, and is therefore not safe to reference in code which are moved between different instances, use Key instead.
Returns the Guid
assigned to the Content during creation. This value is unique, and should never change, even if the content is moved between instances.
// Given a `ContentService` object get Content by its Id and return the Key
var content = contentService.GetById(1234);
return content.Key;
Gets Languages of the Content as a IEnumerable<string>
.
Gets or Sets the given Content
level in the site hierarchy as an Int
. Content placed at the root of the site, will return 1, content right underneath will return 2, and so on.
// Given a `ContentService` object get Content by its Id and return the Level
var content = contentService.GetById(1234);
return content.Level;
Gets or Sets the name of the content as a String
.
// Given a `ContentService` object get Content by its Id and return its Name
var content = contentService.GetById(1234);
return content.Name;
Gets or Sets the parent Content
Id as an Int
.
// Given a `ContentService` object get Content by its Id and return the Id of the Parent Content
var content = contentService.GetById(1234);
return content.ParentId;
Gets or Sets the path of the content as a String
. This string contains a comma separated list of the ancestor Ids including the current contents own id at the end of the string.
// Given a `ContentService` object get Content by its Id and return the Path
var content = contentService.GetById(1234);
return content.Path;
Gets or Sets the IPropertyCollection
object, which is a collection of IProperty
objects. Each property corresponds to a PropertyType
, which is defined on the ContentType
.
// Given a `ContentService` object get Content by its Id and loop through all Properties
var content = contentService.GetById(1234);
foreach (var property in content.Properties)
{
string alias = property.Alias;
object value = property.GetValue();
}
Returns a Bool
indicating whether the given Content
is published and available on the website or not. Notice: the published flag does not check the current in-memory cache, so this flag is not a guarantee that the Content is/is not available in the cache and the frontend of the website.
// Given a `ContentService` object get Content by its Id and return Published
var content = contentService.GetById(1234);
return content.Published;
If set, returns DateTime?
indicating when the Content
should be published and made available on the website and cache.
// Given a `ContentService` object get Content by its Id and set the release date to 4 days from now
var content = contentService.GetById(1234);
return content.PublishDate
Returns the given Content
index, compared to sibling content.
// Given a `ContentService` object get Content by its Id and return its SortOrder
var content = contentService.GetById(1234);
return content.SortOrder;
Returns a IPublishedState
enum with the status of the Content being either Unpublished, Published, Publishing, Unpublishing.
// Given a `ContentService` object get Content by its Id and return its Status
var content = contentService.GetById(1234);
return content.PublishedState;
Gets or sets the template id used to render the content.
// Given a `ContentService` object get Content by its Id and return its Template
var content = contentService.GetById(1234);
return content.TemplateId;
Returns a Bool
indicating whether the given Content
is currently in the recycle bin.
// Given a `ContentService` object get Content by its Id and return Trashed
var content = contentService.GetById(1234);
return content.Trashed;
Gets or Sets a DateTime
object, indicating when the given Content was last updated.
// Given a `ContentService` object get Content by its Id and return UpdateDate
var content = contentService.GetById(1234);
return content.UpdateDate;
Returns the current Version Id as a int
, for each change made to a content item, its values are stored under a new Version. This version is identified by a int
.
// Given a `ContentService` object get Content by its Id and return its Version
var content = contentService.GetById(1234);
return content.VersionId;
Gets or Sets the Id of the User
who made the latest edit on the Content.
// Given a `ContentService` object get Content by its Id and return the Id of the Writer
var content = contentService.GetById(1234);
return content.WriterId;
Gets the IProfile
object for the Creator of this Content, which contains the Id and Name of the User who created this Content item.
// Given a `ContentService` object get Content by its Id and get the `IProfile` of the Creator
var content = contentService.GetById(1234);
var profile = content.GetCreatorProfile(userService);
var id = profile.Id;
string name = profile.Name;
Gets the value of a Property as an Object
.
// Given a `ContentService` object get Content by its Id and get a value by alias
var content = contentService.GetById(1234);
object value = content.GetValue("bodyText");
string text = value as string;
Gets the value of a Property as the defined type 'TPassType'.
// Given a `ContentService` object get Content by its Id and get a value by alias while specifying the return type
var content = contentService.GetById(1234);
string value = content.GetValue<string>("bodyText");
Gets the IProfile
object for the Writer of this Content, which contains the Id and Name of the User who last updated this Content item.
// Given a `ContentService` object get Content by its Id and get the `IProfile` of the Writer
var content = contentService.GetById(1234);
var profile = content.GetWriterProfile(userService);
var id = profile.Id;
string name = profile.Name;
Returns a Bool
indicating whether the Content object has a property with the supplied alias.
// Given a `ContentService` object get Content by its Id and check if certain properties exist
var content = contentService.GetById(1234);
bool tagsExists = content.HasProperty("myTagProperty");
bool bodyTextExists = content.HasProperty("bodyText");
Sets the value of a property by its alias.
// Given a `ContentService` object get Content by its Id, set a few values
// and saves the Content through the `ContentService`
var content = contentService.GetById(1234);
content.SetValue("bodyText", "This text will be added to by RTE field");
content.SetValue("date", DateTime.Now);
contentService.Save(content);
Returns an XElement
containing the Content data, based off the latest changes. Is used when the published content is sent to the in-memory xml cache.
// Given a `ContentService` object get Content by its Id and returns the xml
var content = contentService.GetById(1234);
XElement xml = content.ToXml(serializer);
return xml;