Query 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. 

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:

Query 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

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

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.

Query 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

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

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.

Query 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

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

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.

Query 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

First, you get an instance of the manager. Then, you get the version of the item corresponding to the ID by calling the GetNewsItem method. 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

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.

Handle all 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

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

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.

Query all items of a content type

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

Native API

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

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

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Web Security for Sitefinity Administrators

The free standalone Web Security lesson teaches administrators how to protect your websites and Sitefinity instance from external threats. Learn to configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?

Next article

Edit content