Querying master and live versions

When you query a content item, you can query its live or master version. This topic explains how to get each of these versions. The examples use the ID of the version as an argument. Because the master and the live versions have different unique IDs, the steps you take to get the desired version of the item depend on which of the IDs is passed as an argument. Below you can find code examples for the following scenarios:

You can perform these actions by using either Native API or Fluent API. Therefore, the sections below provide code snippets for both APIs.

NOTE: In the following examples, news items will be used as content. However, the code applies to the following content types:

Querying the live version by the master ID

The following code queries the live version of a content item by the ID of its master version.

Native API

private NewsItem QueryingLiveVersionByMasterIDNativeAPI(Guid masterId)
{
    NewsManager manager = NewsManager.GetManager();
 
    NewsItem master = manager.GetNewsItem(masterId);
 
    NewsItem live = manager.Lifecycle.GetLive(master) as NewsItem;
 
    return live;
}

First, you get an instance of the manager. Then, you get the master version with the specified ID by calling the GetNewsItem method. Finally, to get the live version, you call Lifecycle.GetLive method with the master version as an argument.

NOTE: If an item with the specified masterId does not exist, the GetNewsItem method throws an ItemNotFoundException exception.

Fluent API

private NewsItem QueryingLiveVersionByMasterIDFluentAPI(Guid masterId)
{
    return App.WorkWith().NewsItem(masterId).GetLive().Get();
}

First, you get the singular facade of the master version with the specified ID. Then, to load the live version of the item, you call the GetLivemethod. Finally you call the Get method to get the instance of the live version.

Querying the live version by ID

The following code queries the live version of a content item by the ID of the live version.

Native API

private NewsItem QueryingLiveVersionByLiveIDNativeAPI(Guid liveId)
{
    NewsManager manager = NewsManager.GetManager();
 
    NewsItem item = manager.GetNewsItem(liveId);
 
    return item;
}

First, you get an instance of the manager. Then, you get the version corresponding to the ID by calling the GetNewsItem method. This approach is also used, when you query the master version by its ID.

NOTE: If an item with the specified liveId does not exist, the GetNewsItem method throws an ItemNotFoundException exception.

Fluent API

private NewsItem QueryingLiveVersionByLiveIDFluentAPI(Guid liveId)
{
    return App.WorkWith().NewsItems().Where(item => item.Id == liveId).Get().SingleOrDefault();
}

First, you get the plural facade. Then, you filter the collection to get the item with the corresponding ID.

NOTE: The singular facades for the different content types work only with the master version of the items. If you want to use the ID of the live version, you must use the plural facade for the respective content type and filter the collection, as explained above.

Querying the master version by ID

The following code queries the master version of a content item by the ID of the master version.

Native API

private NewsItem QueryingMasterVersionByMasterIDNativeAPI(Guid masterId)
{
    NewsManager manager = NewsManager.GetManager();
 
    NewsItem item = manager.GetNewsItem(masterId);
 
    return item;
}

First, you get an instance of the manager. Then, you get the version with the specified ID by calling the GetNewsItem method. This approach is also used, when you query the live version by its ID.

NOTE: If an item with the specified masterId does not exist, the GetNewsItem method throws an ItemNotFoundException exception.

Fluent API

private NewsItem QueryingMasterVersionByMasterIDFluentAPI(Guid masterId)
{
    return App.WorkWith().NewsItem(masterId).Get();
}

First, you get the NewsItems singular facade of the item with specified ID. Finally, to obtain an instance of the item, you call the Get method of the facade.

NOTE: The singular facades for the different content types work only with the master version of the items.

Querying the master version by the live ID

The following code queries the master version of a content item by the ID of its live version.

Native API

private NewsItem QueryingMasterVersionByLiveIDNativeAPI(Guid liveId)
{
    NewsManager manager = NewsManager.GetManager();
 
    NewsItem live = manager.GetNewsItem(liveId);
 
    NewsItem master = manager.Lifecycle.GetMaster(live) as NewsItem;
 
    return master;
}

First, you get an instance of the manager. Then, you get the version of the item corresponding to the ID by calling the GetNewsItemmethod. Finally, you call Lifecycle.GetMaster with the live version as an argument.

NOTE: If an item with the specified liveId does not exist, the GetNewsItem method throws an ItemNotFoundException exception.

Fluent API

private NewsItem QueryingMasterVersionByLiveIDFluentAPI(Guid liveId)
{
    NewsItem master = null;
 
    NewsItem live = App.WorkWith().NewsItems().Where(item => item.Id == liveId).Get().SingleOrDefault();
 
    if (live != null)
    {
        master = App.WorkWith().NewsItem().GetManager().GetMaster(live);
    }
 
    return master;
}

First, you get an instance of the live version of the item corresponding to the ID. Then, you get the master version of the item using the instance of the NewsManager exposed by the news item facade.

Handling all the scenarios with one method

The following code handles all the scenarios described above. The method accepts the ID of either the live or the master version and the type of the version that must be returned.

Native API

private NewsItem QueryingContentNativeAPI(Guid itemId, ContentLifecycleStatus status)
{
    NewsManager manager = NewsManager.GetManager();
    NewsItem item = manager.GetNewsItem(itemId);
 
    if (item.Status != status)
    {
        if (item.Status == ContentLifecycleStatus.Live && status == ContentLifecycleStatus.Master)
        {
            item = manager.Lifecycle.GetMaster(item) as NewsItem;
        }
        else if (item.Status == ContentLifecycleStatus.Master && status == ContentLifecycleStatus.Live)
        {
            item = manager.Lifecycle.GetLive(item) as NewsItem;
        }
        else
        {
            //Temp versions of an item must be used only when modifying the item!
            return null;
        }
    }
 
    return item;
}

First, you get an instance of the manager. Then, you get the version of the item with the specified ID by calling the GetNewsItem method. Then, if the status of the item is different from the status argument, you get the version with the desired status. Note that the temp versions of the item are not handled here. You must work with them only when editing an item.

NOTE: If an item with the specified itemId does not exist, the GetNewsItem method throws an ItemNotFoundException exception.

Fluent API

private NewsItem QueryingContentFluentAPI(Guid itemId, ContentLifecycleStatus status)
{
    NewsItem item = App.WorkWith().NewsItems().Where(newsItem => newsItem.Id == itemId).Get().SingleOrDefault();
 
    if (item != null)
    {
        if (item.Status != status)
        {
            if (item.Status == ContentLifecycleStatus.Live && status == ContentLifecycleStatus.Master)
            {
                item = App.WorkWith().NewsItem().GetManager().GetMaster(item);
            }
            else if (item.Status == ContentLifecycleStatus.Master && status == ContentLifecycleStatus.Live)
            {
                item = App.WorkWith().NewsItem().GetManager().GetLive(item);
            }
            else
            {
                //Temp versions of an item must be used only when modifying the item!
                return null;
            }
        }
    }
 
    return item;
}

First, you get an instance of the version of the item with the specified ID using the plural facade. This way the code does not depend on whether the ID belongs to the live or the master version of the item. Then, if the status of the item is different from the status argument, you get the version with the desired status. Note that the temp versions of the item are not handled here. You must work with them only when editing an item.

Querying all items of a content type

The following code queries all news items with the specified status.

Native API

private List<NewsItem> QueryingAllItemsNativeAPI(ContentLifecycleStatus status)
{
    NewsManager manager = NewsManager.GetManager();
 
    return manager.GetNewsItems().Where(item => item.Status == status).ToList();
}

First, you get an instance of the manager. Then, you get all news items and filter them to return only the items with the specified status.

Fluent API

private List<NewsItem> QueryingAllItemsFluentAPI(ContentLifecycleStatus status)
{
    return App.WorkWith().NewsItems().Where(item => item.Status == status).Get().ToList();
}

First, you get all items via the plural facade. Then, you filter them to return only the items with the specified status.

See Also

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK