< Return to Feed
Sean McKaharay - 08.14.2015

Extend xBlog Search

When working with xBlog I came across an issue with xBlog search. I wanted to get a list of all blog post across all the blogs within site. The current way if you wanted to get or search blog posts in xBlog is:

BlogManager.GetBlogPosts(Item currentItem, string categoryID, string authorID, string tagID, string searchText, int startRowIndex, int maximumRows)

What xBlog does it takes the current item and finds current blog and then searches. Below is a way to search all blog post under a home directory:

        public IEnumerable<XBlogHelper.Models.Blog.BlogPost> SearchAllBlogPosts(string categoryID, string authorID, string tagID, string searchText, int startRowIndex, int maximumRows)

        {

            try

            {

                Sitecore.Data.Items.Item repositorySearchItem = Sitecore.Context.Database.Items["/sitecore/content/Home"];

                using (IProviderSearchContext searchContext = ContentSearchManager.GetIndex((IIndexable)new SitecoreIndexableItem(repositorySearchItem)).CreateSearchContext(SearchSecurityOptions.EnableSecurityCheck))

                {

                    Expression<Func<SearchResultItem, bool>> expression = PredicateBuilder.And<SearchResultItem>(PredicateBuilder.True<SearchResultItem>(), (Expression<Func<SearchResultItem, bool>>)(item => item.TemplateName == "Blog Post"));

                    if (!string.IsNullOrEmpty(categoryID))

                        expression = PredicateBuilder.And<SearchResultItem>(expression, (Expression<Func<SearchResultItem, bool>>)(c => c["Category"].Contains(categoryID)));

                    if (!string.IsNullOrEmpty(authorID))

                        expression = PredicateBuilder.And<SearchResultItem>(expression, (Expression<Func<SearchResultItem, bool>>)(a => a["Author"].Contains(authorID)));

                    if (!string.IsNullOrEmpty(tagID))

                        expression = PredicateBuilder.And<SearchResultItem>(expression, (Expression<Func<SearchResultItem, bool>>)(t => t["Tags"].Contains(tagID)));

                    if (!string.IsNullOrEmpty(searchText))

                        expression = PredicateBuilder.And<SearchResultItem>(expression, (Expression<Func<SearchResultItem, bool>>)(t => t["_content"].Contains(searchText)));

                    return (IEnumerable<BlogPost>)Enumerable.ToList<BlogPost>(XBlogHelper.ItemExtensions.CreateAs<BlogPost>(Helper.Slice<SearchResultItem>((IEnumerable<SearchResultItem>)Queryable.OrderByDescending<SearchResultItem, string>(Queryable.Where<SearchResultItem>(searchContext.GetQueryable<SearchResultItem>(), expression), (Expression<Func<SearchResultItem, string>>)(t => t[XBSettings.XBSearchPublishDate])), startRowIndex, maximumRows)));

                }

            }

            catch (Exception ex)

            {

                Log.Error("XBlog Error", ex, new object());

            }

            return (IEnumerable<BlogPost>)null;

        }