Skip to content

IContentRepository Interface

intermediate

IContentRepository provides full read-write access to the content tree. Inject via constructor for content CRUD operations.

Optimizely.Cms.Core

MethodReturnsDescription
Get<T>(ContentReference)TLoads a single content item by reference
Get<T>(ContentReference, CultureInfo)TLoads 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)TCreates a new default instance under a parent
GetDefault<T>(ContentReference, CultureInfo)TCreates a new default instance in a specific language
Save(IContent, SaveAction, AccessLevel)ContentReferenceSaves content with the specified action
Delete(ContentReference, bool)voidDeletes content; true for permanent, false for trash
Move(ContentReference, ContentReference)voidMoves content to a new parent
Copy(ContentReference, ContentReference, AccessLevel, bool)ContentReferenceCopies content to a new parent; bool publishes the copy
ValueDescription
SaveAction.PublishSave and publish immediately
SaveAction.CheckInSave as draft, ready for review
SaveAction.SaveSave without changing status
SaveAction.ForceCurrentVersionOverwrite the current version
SaveAction.SkipValidationFlag; combine with other actions to skip validation
Create and save a page
csharp
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
csharp
// 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
csharp
// Copy and publish the new copy
var copyRef = _repo.Copy(
  sourceRef,
  destinationParentRef,
  AccessLevel.NoAccess,
  publishOnDestination: true
);