Saturday, September 26, 2009

Using the query system in EPiServer Community

The query system in EPiServer Community is a simple way of retrieving entities and collections specific to EPiServer Community. In this blog post I’ll walk through four examples of how the query system can be used.

Let us start with an easy one: Get a user by an ID. This can of course be done by calling the EPiServer.Community.CommunitySystem.CurrentContext.DefaultSecurity.GetUser() method, but let’s see how it can be done by querying:



As we are retrieving a user, we create a query of type UserQuery. All queries have properties of type [x]Criterion, where [x] can be Integer, String, Boolean etc. This means that before setting a property value, we always have to define its criterion type. In this case, as the ID is an int we create an IntegerCriterion and assign it to the userQuery.ID property. We can then set its Value. At last, we call the GetQueryResult() method to get the result of the query.

In the previous example we were only looking for one entity. Now what if we have a list of email addresses, and we want to retrieve all the users with those addresses?



One of the most common mistakes when using the query system is to forget an initialization of an object. If we were to remove the second or third line in the example above, we would get the error message: “Object not set to an instance of an object”. If you’re new to the query system, you’ll probably see this message quite a few times before you get the hang of it, but the only thing it means is that you’ve forgotten to initialize one of the objects.

Now, let’s pick up the pace. Searching is an important feature of every website, and in a search you’ll need to do logical operations. If in a search, you want to retrieve all the clubs with the word “Elephant” in the title OR the description, you’ll need to use a Logical OR operator:



In order to do a logical operation in a query, you need a CriteriaGroup to which you can add all the criteria. In the example above we create a StringCriterion for the clubs Name, and add it to the criteriagroup. We then do the same for the description, but now we specify a LogicalOperator. Finally, we need to add the criteriagroup to the query.

In the final example we’ll see how to query attributes.Let us say you’ve created an attribute for type IUser of type string which specifies the user’s phone number. You now want to retrieve a user based on that phone number:



That’s it for now, happy querying!

6 comments:

  1. Very helpful topic. Thanks a lot!

    ReplyDelete
  2. The CriteriaGroup walkthrough was very helpful. Thanks!

    ReplyDelete
  3. My relatives all the time say that I am wasting my time here at web, except I
    know I am getting experience all the time by reading such good articles.



    Feel free to surf to my web page ... Chaussure De Foot Pas Cher

    ReplyDelete
  4. I need to do task which using UserQuery but need to query club attribute (combine UserQuery and ClubQuery).
    Anyone has suggestion or references, please guide me.
    Thanks!

    ReplyDelete
  5. Hi Karoline,

    How would you do a join?

    Regards,

    Robert.

    ReplyDelete