Content By Query issue with audiences filtering
A few months ago, I came across this issue where the number of returned items in a query wasn’t as expected.
Context with normal queries
To contextualize, let’s say you have a home page with several articles section which are in turned served by Content By Queries. Your queries will either target a specific article sub-site or it will use metadata to query something like :
SELECT TOP 5 Article.Title FROM Article-Site-URL [WHERE MetadataA='value'] ORDER BY LastUpdated DESC
This will show the last 5 updated articles from that site, and if necessary filtered by a metadata.
With daily data being added to all article categories, your home page design is designed to display 5 articles in each category. Usually no less, but certainly no more than 5 items.
Context with audiences
Now if you have an authenticated portal and you’d like to have such an article section with targeted content. For example, the articles are regrouped by a logical business meaning but they are targeted to different groups of users. You will use the same Content By Query and check the “Filter results by audiences” to have only the articles for that user’s audiences. You would expect the “Top 5” to work correctly.
Unfortunately, the query resembles more something like :
SELECT Article.Title FROM (SELECT TOP 5 Article.Title FROM Article-Site-URL ORDER BY LastUpdated DESC) WHERE Article.TargetAudiences CONTAINS (Users.AudiencesGUIDs)
If you look closely at the query, it does a standard TOP 5 query in the content without an audience check. This first query may return 5 items but after the audience filtering, 0 to 5 items will be displayed to the user.
The query should rather be something like :
SELECT TOP 5 Article.Title FROM Article-Site-URL WHERE Article.TargetAudiences CONTAINS (Users.AudiencesGUIDs) ORDER BY LastUpdated DESC
**note: the queries aren’t like this in the APIs, I simply simplified them for explanation**
For a customer using audiences, it can be a show-stopper as you cannot effectively plan your home page queries. The only workaround was to “augment the limit of items return” but since you cannot effectively plan how much you will need, it will break the home page’s layout by either showing too many articles or not enough.
Fix!
Originally, it was apparently by-design and wouldn’t be updated until the next release of Office 14. Fortunately, I’m very happy to have received news that it will be released in an upcoming Cumulative Update along with the fix for multiple audiences explained here.