IContentRepository Interface
Overview
Section titled “Overview”IContentRepository provides full read-write access to the content tree. Inject via constructor for content CRUD operations.
Namespace
Section titled “Namespace”Optimizely.Cms.Core
Methods
Section titled “Methods”| Method | Returns | Description |
|---|---|---|
Get<T>(ContentReference) | T | Loads a single content item by reference |
Get<T>(ContentReference, CultureInfo) | T | Loads content in a specific language |
GetChildren<T>(ContentReference) | IEnumerable<T> | Returns immediate children of a content node |
GetChildren<T>(ContentReference, CultureInfo) | IEnumerable<T> | Returns children in a specific language |
GetAncestors(ContentReference) | IEnumerable<IContent> | Returns all ancestors up to the root |
GetDescendants(ContentReference) | IEnumerable<ContentReference> | Returns references to all descendants |
GetDefault<T>(ContentReference) | T | Creates a new default instance under a parent |
GetDefault<T>(ContentReference, CultureInfo) | T | Creates a new default instance in a specific language |
Save(IContent, SaveAction, AccessLevel) | ContentReference | Saves content with the specified action |
Delete(ContentReference, bool) | void | Deletes content; true for permanent, false for trash |
Move(ContentReference, ContentReference) | void | Moves content to a new parent |
Copy(ContentReference, ContentReference, AccessLevel, bool) | ContentReference | Copies content to a new parent; bool publishes the copy |
SaveAction values
Section titled “SaveAction values”| Value | Description |
|---|---|
SaveAction.Publish | Save and publish immediately |
SaveAction.CheckIn | Save as draft, ready for review |
SaveAction.Save | Save without changing status |
SaveAction.ForceCurrentVersion | Overwrite the current version |
SaveAction.SkipValidation | Flag; combine with other actions to skip validation |
Create and save a page
public class PageService
{
private readonly IContentRepository _repo;
public PageService(IContentRepository repo)
{
_repo = repo;
}
public ContentReference CreateArticle(ContentReference parent, string title)
{
var page = _repo.GetDefault<ArticlePage>(parent);
page.Name = title;
page.Heading = title;
return _repo.Save(page, SaveAction.Publish, AccessLevel.NoAccess);
}
} Move and delete content
// Move a page under a new parent
_repo.Move(pageRef, newParentRef);
// Soft-delete (move to trash)
_repo.Delete(pageRef, forceDelete: false);
// Permanent delete
_repo.Delete(pageRef, forceDelete: true); Copy content
// Copy and publish the new copy
var copyRef = _repo.Copy(
sourceRef,
destinationParentRef,
AccessLevel.NoAccess,
publishOnDestination: true
); Related
Section titled “Related”- IContentLoader Interface — Read-only content loading
- IContent Interface — Base content interface
- Content Events Reference — Hook into save/publish lifecycle