November Happy Hour will be moved to Thursday December 5th.

Querying blogs in a set of specified clubs

Vote:
 

Hi,

 

Do you have an example of how to query a number of blogs that belong to a certain club or clubs?

I cannot work out how to achieve this with the docs and available examples.  Users are required to search for content within a set of clubs.

 

Thanks,

Simon

#28215
Feb 25, 2009 13:39
Vote:
 

Hi Simon,

I think there are two solutions for your problem. The easiest one is to add an attribute to each blog instance called something like "clubId". When you create a club, set the clubId for the blog that is created - this is best done in a HTTP module that listens to the Club created event. Now you can search for this attribute.

The other way is to build a custom Criteria and in this criteria make a join with clubs.

Best regards,
Tom

#28254
Feb 27, 2009 16:55
Vote:
 

Having some more query related problems...

 

We have a blog that has an Attribute called 'BelongsToClub' I want to pass a number of possible clubs it can belong to, I can get this working fine.

 

entryQuery["BelongsToClub"] = GetClubCriterion(communitIDs) //returns ClubCriterion()

 

I also want to add the following

IntegerCriterion intC1 = new IntegerCriterion();
intC1.Value = (int)EPiServer.Community.Blog.BlogType.UserBlog;
IntegerCriterion intC2 = new IntegerCriterion();
intC2.Value = (int)EPiServer.Community.Blog.BlogType.ClubNews; 

BlogCriterion blog1 = new BlogCriterion();
blog1["BlogTypeID"] = intC1;
BlogCriterion blog2 = new BlogCriterion();
blog2["BlogTypeID"] = intC2;

CriteriaGroup cgBlog = new CriteriaGroup();
cgBlog.AddCriterion(blog1);
cgBlog.AddCriterion(LogicalOperator.Or, blog2);
entryQuery.AddCriteriaGroup(cgBlog);

 

When this runs I get the following error :

A criterion in the supplied ICriteriaGroup is not defined in this Query.
Parameter name: group
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: A criterion in the supplied ICriteriaGroup is not defined in this Query.
Parameter name: group

Source Error:

Line 483: cgBlog.AddCriterion(blog1);
Line 484: cgBlog.AddCriterion(LogicalOperator.Or, blog2);
Line 485: entryQuery.AddCriteriaGroup(cgBlog);

 

 

Any ideas?....  i've been finding some of this querying language quite hard to work out!


Thanks,

Simon

#31205
Jul 08, 2009 18:24
Vote:
 

Ok, i've got most of that working, but where i'm really stuck now is on CriteriaGroup.

How do you group items together that are on different levels? eg.

 

entryQuery["BelongsToClub"] is set to the correct ClubCriterion that brings the results back i want.

then I set entryQuery.Blog["BlogTypeID"] to the IntegerCriterion i want, that also brings back the correct results on its own.

 

I want to group those two items together, using LogicalOperator.Or, does that make sense?  How do I do that, i've tried everything i can think of...

Thanks,

Simon

#31232
Jul 10, 2009 12:33
Vote:
 

I've been trying to solve the issue of getting blog entries with title and content values which contain specified keywords AND belong to specified clubs AND have a blogType='ClubNews' OR
blogType = 'UserBlog'. I have coded the following which under limited testing appears to work - although its not pretty it might help :-)

It requires using System.Collections.Generic; using EPiServer.Community.Blog; using EPiServer.Community.Blog.Queries; 

        public EntryCollection SearchTopicsEntries(string searchText, int[] blogIds)
        {
            // Search Blogs

            EntryCollection entryCollection = new EntryCollection();
            EntryCollection result = new EntryCollection();

            EntryQuery eq = new EntryQuery();

            // Create title criteria

            StringCriterion titleCriterion = new StringCriterion();
            titleCriterion.Value = searchText;
            titleCriterion.WildCardType = WildCardType.Both;
            eq.Title = titleCriterion;

            // Create content criteria

            StringCriterion contentCriterion = new StringCriterion();
            contentCriterion.Value = searchText;
            contentCriterion.WildCardType = WildCardType.Both;
            eq.Content = contentCriterion;

            // Create and add criteriaGroup
           
            CriteriaGroup criteriaGroup = new CriteriaGroup();
            criteriaGroup.AddCriterion(eq.Title);
            criteriaGroup.AddCriterion(LogicalOperator.Or, eq.Content);

            eq.AddCriteriaGroup(criteriaGroup);

            // Get searched for entries in with keyword in title or contents 

            int totalTopicItems = 0;
            entryCollection = QueryHandler.GetQueryResult<Community.Blog.Entry, EntryCollection>(eq, Utils.CacheTimeOut.Search, 1, 12, out totalTopicItems);
           
            // Filter returned entryCollection for blogs with the right ID values and blogType

            IList<int> blogIdsAsList = blogIds as IList<int>;

            foreach (Entry entry in entryCollection)
            {
                int blogId = entry.Blog.ID;
                if (blogIdsAsList.Contains(blogId))
                {
                    if (BlogOfTypeRequired(blogId))
                    {
                        // if blog has ID = blogId and blogtype UserBlog or ClubNews add to result,
                        result.Add(entry);
                    }
                }
            }
            return result;
        }

        public bool BlogOfTypeRequired(int blogId)
        {
            bool result = false;

            BlogCollection blogCollection = new BlogCollection();
            BlogQuery bq = new BlogQuery();

            // Create ID criterion
           
            IntegerCriterion idCriterion = new IntegerCriterion();
            idCriterion.Value = blogId;
            bq.ID = idCriterion;

            // Create first BlogTypeCriterion - UserBlog

            BlogTypeCriterion userBlogCriterion = new BlogTypeCriterion();
            userBlogCriterion.Value = BlogType.UserBlog;
            bq.BlogType = userBlogCriterion;

            // Create and add criteriaGroup

            CriteriaGroup criteriaGroup = new CriteriaGroup();
            criteriaGroup.AddCriterion(bq.ID);
            criteriaGroup.AddCriterion(LogicalOperator.And, bq.BlogType);

            bq.AddCriteriaGroup(criteriaGroup);

            // Collect results

            int totalTopicItems = 0;
            blogCollection = QueryHandler.GetQueryResult<Community.Blog.Blog, BlogCollection>(bq, Utils.CacheTimeOut.Search, 1, 12, out totalTopicItems);

            if (blogCollection.Count > 0)
            {
                result = true;
            }
            else
            {
                // try ClubsNews as a BlogType
               
                userBlogCriterion.Value = BlogType.ClubNews;
                criteriaGroup = new CriteriaGroup();
                criteriaGroup.AddCriterion(bq.ID);
                criteriaGroup.AddCriterion(LogicalOperator.And, bq.BlogType);
                bq.AddCriteriaGroup(criteriaGroup);

                // Collect the results

                totalTopicItems = 0;
                blogCollection = QueryHandler.GetQueryResult<Community.Blog.Blog, BlogCollection>(bq, Utils.CacheTimeOut.Search, 1, 12, out totalTopicItems);

                if (blogCollection.Count > 0)
                {
                    result = true;
                }
            }

            return result;
        }

#31381
Jul 24, 2009 11:33
This thread is locked and should be used for reference only. Please use the Legacy add-ons forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.