5

I'm trying to build a query to give me components based on a set of critiera, I think i can explain it better by referencing the following image:

enter image description here

My query looks like this:

  • "Year" IS 2014 OR 2013
  • "Brand" IS Sony

So I should get 2 components returned

I'm first building my criteria for the 'years' like so:

criteriaList.Add(new KeywordCriteria(yearTaxonomyId, yearIds.ToArray()));

Then adding the 'And' for the brand like so:

criteriaList.Add(new TaxonomyKeywordKeyCriteria(publicationId, BrandTaxonomyTcm.ItemId, "Sony", false));

The executing the query, i've tried combinations of OrCriteria / AndCriteria

Criteria criteria = new AndCriteria(criteriaList.ToArray());
Query query = new Query(criteria);

This isn't giving me any results :( - I've managed to get results when just one Year based on the AndCriteria (using similar code), i'm just looking for the most efficient (and working :)) way to perform this query and get my results.

5

Here are a couple of issues with your logic:

  1. new KeywordCriteria(yearTaxonomyId, yearIds.ToArray()) creates a criteria where an item is tagged with all the Keywords in your array. You seem to be looking for OR here. So you will need to create individual KeywordCriteria for each year and OR them together.

  2. new KeywordCriteria(yearTaxonomyId, yearIds.ToArray()) should pass in Category name, not ID. If all you have is ID, then stick to TaxonomyKeywordKeyCriteria (but don't forget to publish the Category if you use this one). Also make sure your Keywords have the Key specified. If they don't, then use TaxonomyKeywordNameCriteria

Here is some code:

KeywordCriteria[] yearKeywordCriterias = new KeywordCriteria[yearIds.Count]();
int i=0;
foreach(var year in yearIds) {
    yearOrCriteria[i] = new KeywordCriteria("NAME OF YOUR CATEGORY, NOT ID FOO!", year.ToString());
    i++;
}
OrCriteria yearOrCriteria = new OrCriteria(yearKeywordCriterias);

List<Criteria> criteriaList = new List<Criteria>();
criteriaList.Add(yearOrCriteria);    
criteriaList.Add(new TaxonomyKeywordKeyCriteria(publicationId, BrandTaxonomyTcm.ItemId, "Sony", false));
Query query = new Query(criteria);
  • 2
    thanks nick, i also needed to ensure i was in the scope of the same publication with "criteriaList.Add(new PublicationCriteria(publicationId));" and job is a good un' – johnwinter Feb 4 '14 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.