Querying blog posts

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

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

Querying a single blog post

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

  1. Get the master version.

    First, get an instance of the master version with the specified ID. 

  2. Get the live version.

    The instance that corresponds to the ID argument is the master version of the blog post. You must get the live version explicitly.

    NOTE: The live version is used only when displaying the blog posts in a front-end scenario. If you want to manipulate the blog post, you must use the master version. To transfer the changes to the live version, publish the master version. For more information, see Modifying blog posts.

  3. Return the live version.

    Return the live version. 

The following code queries the live version of a blog post by the ID of its master version.

Native API

private BlogPost GetBlogPostNativeAPI(Guid masterBlogPostId)
{
    BlogsManager blogsManager = BlogsManager.GetManager();
  
    //Get the master version.
    BlogPost blogPost = blogsManager.GetBlogPosts().Where(b => b.Id == masterBlogPostId).FirstOrDefault();
  
    if (blogPost != null)
    {
        //Get the live version.
        blogPost = blogsManager.Lifecycle.GetLive(blogPost) as BlogPost;
    }
  
    return blogPost;
}

First, you get an instance of the BlogsManager class. You get the specified blog post by querying all blog posts and filtering the collection by the ID of the blog post. 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 live version.

You can also use the following code to retrieve the blog post:

BlogPost blogPost = blogsManager.GetBlogPost(masterBlogPostId);

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

Fluent API

private BlogPost GetBlogPostFluentAPI(Guid masterBlogPostId)
{
    return App.WorkWith().BlogPost(masterBlogPostId).GetLive().Get();
}

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

Querying all blog posts

When querying all blog posts, you must perform the following:

  1. Query all blog posts.

    First, get a query of all available blog posts. 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 blog posts.

    Get the filtered items in a list. 

  4. Return the blog posts.

    Return the list of blog posts. 

The following code queries all blog posts.

Native API

private List<BlogPost> GetAllBlogPostsNativeAPI()
{
    BlogsManager blogsManager = BlogsManager.GetManager();
  
    return blogsManager.GetBlogPosts().Where(b => b.Status == ContentLifecycleStatus.Live).ToList();
}

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

To get the blog posts from a specific blog, use the following code:

private List<BlogPost> GetAllBlogPostsByBlogNativeAPI(Guid blogId)
{
    BlogsManager blogsManager = BlogsManager.GetManager();
  
    return blogsManager.GetBlogPosts().Where(b => b.Status == ContentLifecycleStatus.Live && b.Parent.Id == blogId).ToList();
}

In this code example you additionally filter the collection by the ID of the parent blog.

Fluent API

private List<BlogPost> GetAllBlogPostsFluentAPI()
{
    return App.WorkWith().BlogPosts().Where(b => b.Status == ContentLifecycleStatus.Live).Get().ToList();
} 

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

To get the blog posts from a specific blog, use the following code:

private List<BlogPost> GetAllBlogPostsByBlogFluentAPI(Guid blogId)
{
    return App.WorkWith().Blog(blogId).BlogPosts().Where(b => b.Status == ContentLifecycleStatus.Live).Get().ToList();
}

In this code example you use the singular facade for the specified blog and get the live versions of its blog posts.

See Also

Next steps

+1-888-365-2779
sales@sitefinity.com

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