4

I would like to use the keywords from a specific category as a filter in an authorisation scheme. The web application will give me an array of "claims" that can be matched to keywords out of the category, and the published items will be tagged with keywords representing the claims that are necessary to allow them to be viewed. If an item is not tagged with any keywords out of the category, then it should be visible to all visitors.

How would I construct a broker querying "criteria" to represent this. In other words:

Return the items that have any of these keywords, or none

The point being that if any claim is required, you must be able to match it.

I don't want to have a keyword that represents "Everyone".

  • can you just take the Keyword Criteria out of the query if there are none? – Nickoli Roussakov Sep 3 '15 at 14:06
  • No, because the visitor may have claims that don't apply, but they should still see items without any keyword. Of course, I could just process the whole list, but it's much more elegant if there's a way to express it in the criteria. – Dominic Cronin Sep 3 '15 at 14:14
5

You could use a KeywordCriteria with the category and all of the keywords and the NotEqual field operator:
var noKeywordsAttached = new KeywordCriteria(myCategory, allKeywordsInMyCategory, Criteria.NotEqual)

The obvious drawback being that you would have make an additional taxonomy query to determine all keywords in the category.

This combined with your claims based matches:
var matchesClaimsKeyword = new KeywordCriteria(myCategory, keywordsFromClaims, Criteria.Equals)

and finally chaining them using an OrCriteria:
new OrCriteria(noKeywordsAttached, matchesClaimsKeyword)

| improve this answer | |
  • That isn't really a drawback, as otherwise I'll end up getting more results than I want and having to query their meta directly to filter them in place. – Dominic Cronin Sep 4 '15 at 14:00
  • In the end I couldn't get this to work, but it pointed me in the right direction. I now have a working query with a NotInCriteria – Dominic Cronin Sep 14 '15 at 13:40

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.