Querying news items

This topic explains how to query instances of news items. The examples below show you how to query all of the available news items or how to query only a specific item by its ID.

NOTE: The code examples below work with the ID of the master version of the item and return the live version of the item. For more information about other scenarios, see Querying master and live versions.

Querying a single news item

When querying the live version of a specific news item by the ID of its master version, you must perform the following:

  1. Get the item.

    First, get an instance of the master version of the item that corresponds to the specified ID. 

  2. Get the live version.

    The instance that corresponds to the ID argument is the master version of the news item. You can get the live version explicitly.

    NOTE: The live version is used only when displaying the news in a front-end scenario. If you want to manipulate the news item, consider using the master version. To transfer the changes to the live version, publish the master version.

  3. Return the item.

    Return the news item. 

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

Native API

rivate NewsItem GetNewsItemNativeAPI(Guid masterNewsId)
{
    NewsManager newsManager = NewsManager.GetManager();
    NewsItem item = newsManager.GetNewsItems().Where(newsItem => newsItem.Id == masterNewsId).FirstOrDefault();
  
    if (item != null)
    {
        item = newsManager.Lifecycle.GetLive(item) as NewsItem;        
    }
  
    return item;
}

First, you get an instance of the NewsManager class. You get the specified news item by querying all items and filtering the collection by the ID of the news item. If the item exists, you get its live version. If no live version exists (i.e. the item has not been published), you return null. Finally, you return the news item.

You can also use the following code to retrieve the news item:

NewsItem item = newsManager.GetNewsItem(newsId);

Note that when the item does not exist, an exception of type ItemNotFoundException is thrown.

Fluent API

private NewsItem GetNewsItemFluentAPI(Guid masterNewsId)
{
    return App.WorkWith().NewsItem(masterNewsId).GetLive().Get();
}

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

Querying all news items

When querying all news items, you must perform the following: 

  1. Query all items.

    First, get a query of all available news items. The query includes all live, master and temp versions. 

  2. Filter the query.

    Filter the query to return only the live versions.

  3. Get the items.

    Get the filtered items in a list.

  4. Return the items.

    Return the list of news items. 

The following code queries all news items.

Native API

private List<NewsItem> GetAllNewsItemsNativeAPI()
{
    NewsManager newsManager = NewsManager.GetManager();
  
    return newsManager.GetNewsItems().Where(newsItem => newsItem.Status == ContentLifecycleStatus.Live).ToList();
}

First, you get an instance of the NewsManager class. You query all the news items, including the live, master and temp versions of each. You filter the collection to return only the live versions. Finally, you return the news items as a list.

Fluent API

private List<NewsItem> GetAllNewsItemsFluentAPI()
{
    return App.WorkWith().NewsItems().Where(newsItem => newsItem.Status == ContentLifecycleStatus.Live).Get().ToList();
}

First, you get an instance of the plural news facade. Then you get all the news items, including the live, master and temp versions of each. You filter the collection to return only the live versions. Finally, you return the news items as a list. If no news items exist or there aren't any published news items, you return an empty list.

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