Filter a Content Search Webpart by a User Profile Property

As you all know the Content Search webpart is perfect for aggregating data regardless of the site where the data has been stored. In other words: with the Content Search webpart you can create a dashboard with aggregated data very easily.

But you can only display the aggregated data in a static manner. Although you can use the wizard that is found in the webpart properties to easily create a search query, this query is a static query. There is no way to add an additional filter parameter to neither the query nor the webpart itself while the Content Search webpart is displaying data. Let’s look on a generic SharePoint list to illustrate this. With a list a user can use filtering while the list is displaying its data. With the Content-Search webpart a user can’t do that. Usually the Content-Search webpart is displaying data in a static manner.

But there is a way to build a personal dashboard with the Content-Search webpart. You can use a User-Profile property to enable the Content-Search webpart to display only data that is relevant for the current user.

Let me show you how to do that.

First of all I create a Term-Set that holds several locations. My Term-Set looks like this:

Content Search Webpart

Next I add another User-Profile property to the existing set of properties and link it to the Term-Set I have just created. My new property looks like this:

Oliver2

In this demo I’m using the administrator account and fill the newly created User-Profile property with an appropriate location – let’s move the Administrator to Vancouver 🙂

Oliver3

Next I create a new Content-Type. For this demo I just add two additional fields: a description and a Managed-Metadata field that is also linked to the Term-Set ‘Location’. My Content-Type looks like this:

Oliver4
A Content-Type is nothing without a list, so I create a generic list and attach the newly created Content-Type to that list. After that I add 4 new items, each of them with a different location.

Oliver5

Now that the data is existing it’s time to configure the Content-Search webpart. The basic configuration is easy: the Content-Search webpart should only display data that matches the Content-Type I have just created.

Oliver6

With this configuration the Content-Search webpart is able to retrieve the data – you can use the preview on the right side of the query editor to verify this. I have cut off the preview in the above screenshot.

After that I need to configure the way the Content-Search webpart should display the data. I decide to use the simple 2-lines template. First line shows the title, second line shows the location (you need to use the crawled property that matches the location here.).

With this simple configuration the Content-Search webpart displays the list items like this – as expected 🙂

Oliver7

That’s the static way to display aggregated data. Now I want to add an additional filter that ensures that only items are displayed that have a location that matches the location in the User-Profile property ‘Location’ of the current user.

To do this you only need to add an additional property filter the way I have done it in the following screenshot.

Oliver8

I add this additional filter: owstaxIdLocation:{User.Location}. ‘owstaxIdLocation’ is the name of the crawled property in my demo. In your environment the name might be different. {User.Location} means: “take the current user’s properties and look for a property called ‘Location’”. The operator ‘:’ means “is equal”.

As you can see in the preview only one data item is displayed. Without this additional filter four items are shown. I save the changed settings and publish the page again – and this is what the Content-Search webpart is displaying now:

Oliver9

As expected the Content-Search webpart now only shows one single item – the item with location that matches the location of the current user.

Let’s have a look on the Content-Search webpart if a user has not provided a location.

Oliver 10Nothing is displayed yet as the current user does not have a valid location. As soon as this user enters a valid location, the Content-Search webpart will show items for this user as well.

Besides a User-Profile property there are a few other ways of including a kind of dynamic filter. Have a look at the following screenshot:

Oliver 11

‘Parameter from URL’ and ‘Token from URL’ are often used if the Content-Search webpart is used to display items of a catalog of goods.

I hope that I was able to show that the Content-Search webpart can not only be used to display data items based on a static query.

Here are some additional references:

Keyword Query Language (KQL) syntax reference
Configure the query in a Content search webpart on a category stage

For more informative content on Search why not check out this Oliver Wirkus blog on ‘SharePoint: Using the Keyword Query for Search Driven Applications’. Read the blog now>>

Share this on...

Rate this Post:

Share:

Topics:

General