Update: 04/03/2019 – Microsoft have now added filters to the input fields in the Highlighted Content web part, the below approach no longer works.

Update: Microsoft have now implemented metadata support into pages and news web parts now contain filters to achieve this without the below approach.

We have built a light-weight Intranet using Communication Sites for a client that they needed the ability to categorise and display different types of pages and news. This article describes how to insert in more advanced queries (KQL) into a modern “Highlighted Content” web part (which is a little hacky) to extract specific categories and to setup page categorisation.

For demonstrating the blog post, we will use a Microsoft Demo tenant and go to the group called “Electronic Events”. Btw, these are great if you want to demonstrate capability to a client or play with a feature without affecting your live environment, you can create a 90 day tenant that even includes lots of sample content, groups, emails, based on the Contoso organisation.

Setup simple page categorisation

You can achieve simple categorisation to allow you to group pages by using metadata. Specifically, we use the Term Store to categorise pages.

Firstly, to go the “Term Store” and add some simple category terms. For this example we want to categorise the pages by “Charity”, “City” and “Showcase”.

Term Store
Example set of terms to categorise pages

Next, we need to go to the site settings and create a new “Managed Metadata” site column that references this new term set, call this column “NewsCategory” for example. Note: as a principle, we always create a column without a space and rename later, that way the Internal name is nice and clean e.g. NewsCategory as opposed to News_x0020_Category (< Yuk).

Selected the new term set “News Categories”

Right, now let’s go and create a content type called “Categorised Site Page”, when you select which parent to inherit from choose “Document Content Types” > “Site Page” – this is the standard content type for all modern page; then add the new managed metadata site column you have just created.

Create Content Type
Creating new content type based on Site Page

In your site (Office365 group or Communication Site), go to the Site Pages Library and edit the settings. You will find there are already a few content types on the library, go ahead and add the new Content Type and set this to be the default Content Type.

Add Content Type to list and set default
Added new content type to Site Pages

Whilst you are in the library, create a view with the News Category column, you are going to need this to use “Quick Edit” to add the metadata to the items.

Firstly, edit each item and change the content type to “Categorised Site Page”. It’s a great use for the new feature to “bulk edit”, which I find is a very welcome addition to the modern toolset.

Bulk Update Content Type
Update all items using bulk edit to change content type to “Categorised Site Page”

Now let’s use the Quick edit feature to update all the list items quickly to add a new category, like this below:

Site Page Categoried Added
Setting News Category Column with Quick Edit

Remember to “publish” each page to ensure that the column get mapped in the Search Index. This may take a while so come back to this in 15-30 minutes or so.

Using the Highlighted Content to display specific categories

For this example, we want to filter the “Showcase” and “Charity” news articles to make this prominent on the page. I really needed to use the OR clause but there wasn’t that option in the web part for that; however we have a workaround! By injecting a little KQL into the value field, in the filter and sort section.

Let’s add a “Highlighted Content” web part to the page, set the Document Type to News and go to the Filter and Sort Section, select “Managed Property” and start to type “owstaxid” this will start to auto filter the list on the dropdown below and select “owstaxidNewsCategory”, set the operator to “Contains” and the value to “City”, like a simplified Query builder.

Using the F11 tools in Edge, we can observe the query payload for this query, look for “postquery” in the name of the call – the first part of the value payload looks like this:

"Value":"owstaxIdNewsCategory:\"City\" AND  ...

As you can see with the use of the escaping(\”) that the web part does with the value fields, you have the option to inject further KQL into the value field to perform queries like the OR clause, which will allow you to filter with multiple categories.

Now change the query to the following value (noting the use of the speech marks):

City" OR owstaxidNewsCategory:"Charity

Observing what happens in the “postquery” response, the value payload becomes:

"Value":"owstaxIdNewsCategory:\"Showcase\" OR owstaxIdNewsCategory:\"Charity\" 
AND ...

Which then now allows you to restrict the articles by category:

Final Result.jpg
Just look at the final result in the “Awesome News” section


On reflection…

Whilst we are not overly happy with the fact this kind of manipulation was required – we feel that allowing KQL queries as an advanced option would change the power of the web part tremendously.

Remember, Microsoft love to change things, this may not work in the future when Microsoft enhance the web part.