31.10.2014 Views

Developer's Guide - EPiServer World

Developer's Guide - EPiServer World

Developer's Guide - EPiServer World

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

20 | Developer’s <strong>Guide</strong> <strong>EPiServer</strong> Community 3.1<br />

2.6.2 Filter on custom attributes<br />

This is a simple example of how to filter on an attribute. In this case it’s a primitive string attribute, but it could very well<br />

also be a complex attribute. If for example it was a Forum attribute, a ForumCriterion would be set instead of a<br />

StringCriterion. Nesting of this criterion would then also be possible.<br />

//Create a blog query<br />

BlogQuery bq = new BlogQuery();<br />

StringCriterion strCriterion = new StringCriterion();<br />

strCriterion.Value = "Stringvalue";<br />

bq["stringattrib"] = strCriterion;<br />

bq.Author = new AuthorCriterion();<br />

bq.Author.Name = new StringCriterion();<br />

bq.OrderBy.Add(<br />

new CriterionSortOrder(bq.Author.Name, SortingDirection.Ascending));<br />

BlogCollection blogs = BlogHandler.GetQueryResult(bq);<br />

2.6.3 Using And / Or conditions<br />

Criteria can be grouped and delimeted with And / Or. In this example we group the Name and NumEntries criteria,<br />

which will return Blogs with the name “Blog Test” OR with 7 entries.<br />

//Create a new BlogQuery<br />

BlogQuery bq = new BlogQuery()<br />

//Initialize criterions<br />

bq.Name = new StringCriterion();<br />

bq.NumEntries = new IntegerCriterion();<br />

bq.Author = new AuthorCriterion();<br />

bq.Author.Name = new StringCriterion();<br />

//Set values to filter on<br />

bq.Name.Value = "Blog Test";<br />

bq.NumEntries.Value = 7;<br />

// We group Name and NumEntries and put OR inbetween<br />

CriteriaGroup cg = new CriteriaGroup();<br />

cg.AddCriterion(bq.Name);<br />

cg.AddCriterion(LogicalOperator.Or, bq.NumEntries);<br />

bq.AddCriteriaGroup(cg);<br />

//Order by author name<br />

bq.OrderBy.Add(<br />

new CriterionSortOrder(bq.Author.Name, SortingDirection.Ascending));<br />

//Get the filtered blog collection<br />

BlogCollection blogs = BlogHandler.GetQueryResult(bq);<br />

© <strong>EPiServer</strong> AB

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!