1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Bugs & Issues / Can't retrieve news item using fluent API

Can't retrieve news item using fluent API

4 posts, 1 answered
  1. Thomas
    Thomas avatar
    223 posts
    Registered:
    19 Jan 2011
    18 Aug 2011
    Link to this post
    Hi,

    I'm a bit confused about when to use the fluent API or content managers. I just try one, and if it fails, use the other one. But I suspect that both are supposed to work, and if not, it's a bug.

    For example, the following piece of code doesn't work and throws a System.InvalidOperationException ("Telerik.Sitefinity.News.Model.NewsItem with id xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is not a draft."):

    var newsItem = App.WorkWith().NewsItem(id).Get();

    Same result if I try something a bit different, like:
    var newsItem = App.WorkWith().NewsItems().Publihed().FirstThat(n => n.Id == id).Get();

    However, this works fine:

    var newsItem = NewsManager.GetManager().GetNewsItem(id);

    This is with SF 4.2.
  2. Svetoslav Petsov
    Svetoslav Petsov avatar
    456 posts
    Registered:
    24 Sep 2012
    22 Aug 2011
    Link to this post
    Hi Thomas,

     The reason why you can't access a published NewsItem with App.WorkWith().NewsItem(id) is because it provides the facade for draft elements (and yours is published). When you use App.WorkWith().NewsItems() this is a plural facade for all News elements and when your Get one of them, it has a Draft status (up until you publish it). The already published item is kept in a master object, which is replaced with the new Drafted one after publishing. This means that when you get the News Item (by title for example) and then get its ID, it returns the drafted ID (and NewsItems().Published() doesn't find it as a published item). 
    So, if you need to find the item by the ID (and it is published) you will have to use the following code.

    var newsItem = App.WorkWith().NewsItems().FirstThat(p => p.ID == id).Get();
    but I will recommend to use the Where - First pattern:

    var newsItem = App.WorkWith().NewsItems().Where(t => t.ID == id).First().Get();

    More about the Fluent Api here:
    http://www.sitefinity.com/40/help/developers-guide/deep-dive-fluent-api-overview.html
    Also, info on querying newsItems here:
    http://www.sitefinity.com/40/help/developers-guide/sitefinity-essentials-modules-news-querying-news-items.html

    All the best,
    Svetoslav Petsov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

    Answered
  3. Thomas
    Thomas avatar
    223 posts
    Registered:
    19 Jan 2011
    22 Aug 2011
    Link to this post
    Hi Svetoslav,

    Thanks for the explanation. Confusing, though...
    Out of curiosity, why do you recommend Where + First rather than FirstThat?
  4. Svetoslav Petsov
    Svetoslav Petsov avatar
    456 posts
    Registered:
    24 Sep 2012
    22 Aug 2011
    Link to this post
    Hi Thomas,

     Not anything in particular, just the Where() . First() methods are more popular and there is a smaller possibility for them to cause problems. 

    All the best,
    Svetoslav Petsov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

4 posts, 1 answered