This content is archived. See latest version here

Last updated: Mar 31 2014

Table of contents

Introduction

Through IContentRepository it is possible to save and load IContent instances. The most typical content instance is types inheriting PageData, although it is possible to load and save other content types than pages. These content instances will not show up in the page tree in the editorial interface.

One use case could be for example when implementing comment functionality. In that case each comment could be saved as an IContent under the page that is commented on. However, the comments will not be seen in page tree.

Below is an example on how such a Comment type could be defined and persisted.

Defining comment content type

The following code shows the Comment class:

C#
[ContentType]
public class CommentUser : BlockData
{
    public virtual string Email { get; set; }
    public virtual string UserName { get; set; }
}

[ContentType]
public class Comment : IContent
{
    public virtual CommentUser User { get; set; }
    public virtual string Body { get; set; }

    #region IContent
    private PropertyDataCollection _properties = new PropertyDataCollection();

    public string Name { get; set; }
    public ContentReference ContentLink { get; set; }
    public ContentReference ParentLink { get; set; }
    public Guid ContentGuid { get; set; }
    public int ContentTypeID { get; set; }
    public bool IsDeleted { get; set; }
    public PropertyDataCollection Property
    {
        get { return _properties; }
    }
    public bool IsNull
    {
        get { return _properties.Count == 0; }
    }
    #endregion
}

Registering ContentType

Each IContent that is stored in the CMS content database must have a corresponding ContentType registered. Classes annotated with the ContentType attribute will be automatically scanned for and registered during initialization.

Saving and loading comment

The instance of the interface IContentRepository can be used to create, load and save content instances. The below code shows how to create a Comment instance, save it and load it back.

C#
public class CommentHandler
{
    private IContentRepository _contentRepository;
    public CommentHandler(IContentRepository contentRepository)
    {
        _contentRepository = contentRepository;
    }

    public Comment CreateAComment()
    {
        var comment = _contentRepository.GetDefault<Comment>(PageReference.RootPage, LanguageSelector.AutoDetect());
        comment.Name = "acomment";
        comment.User.Email = "user@test.com";
        comment.Body = "This is a comment";

        var contentLink = _contentRepository.Save(comment, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);
        var loadedComment = _contentRepository.Get<Comment>(contentLink);

        System.Diagnostics.Debug.Assert(comment.User.Email == loadedComment.User.Email);
        System.Diagnostics.Debug.Assert(comment.Body == loadedComment.Body);

        return comment;
    }
}

See also


Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/