IPublishedStateAssessor Interface
Overview
Section titled “Overview”IPublishedStateAssessor determines the published state of content items by evaluating StartPublish, StopPublish, and Status properties. Inject it to check content visibility without manual date comparisons.
Namespace
Section titled “Namespace”Optimizely.Cms.Core
Methods
Section titled “Methods”| Method | Returns | Description |
|---|---|---|
IsPublished(IContent) | bool | Returns true if the content is published and within its publish window |
IsPublished(IContent, DateTime) | bool | Checks published state at a specific point in time |
IsExpired(IContent) | bool | Returns true if StopPublish has passed |
CheckPublishedStatus(IContent) | PublishedStatus | Returns the detailed publish status enum |
PublishedStatus enum
Section titled “PublishedStatus enum”| Value | Description |
|---|---|
Published | Content is published and within its publish date range |
NotPublished | Content has never been published |
Expired | StopPublish date has passed |
NotYetPublished | StartPublish date is in the future |
Draft | Content is saved as a draft |
Publish window evaluation
Section titled “Publish window evaluation”The assessor evaluates these IVersionable properties:
| Property | Effect |
|---|---|
StartPublish | Content is not visible before this date |
StopPublish | Content is not visible after this date |
Status | Must be VersionStatus.Published |
If StartPublish is null, the content is visible immediately upon publish. If StopPublish is null, the content never expires.
Check published state
public class ContentVisibilityService
{
private readonly IPublishedStateAssessor _assessor;
private readonly IContentLoader _loader;
public ContentVisibilityService(
IPublishedStateAssessor assessor,
IContentLoader loader)
{
_assessor = assessor;
_loader = loader;
}
public bool IsVisibleToVisitors(ContentReference contentRef)
{
var content = _loader.Get<IContent>(contentRef);
return _assessor.IsPublished(content);
}
public string GetStatusLabel(IContent content)
{
return _assessor.CheckPublishedStatus(content) switch
{
PublishedStatus.Published => "Live",
PublishedStatus.Expired => "Expired",
PublishedStatus.NotYetPublished => "Scheduled",
PublishedStatus.Draft => "Draft",
_ => "Unpublished"
};
}
} Filter visible content
// Filter a list to only published items
var publishedItems = allItems
.Where(item => _assessor.IsPublished(item))
.ToList();
// Check if content will be live at a future date
var isLiveNextWeek = _assessor.IsPublished(content, DateTime.Now.AddDays(7)); Related
Section titled “Related”- IContent Interface — Base content interface with IVersionable
- IContentRepository Interface — Save with publish actions
- Content Events Reference — PublishingContent / PublishedContent events