User Context Sensitive Searching In SharePoint 2013 Part 2

This is the second post in a two part series.  The purpose of this two part series is to demonstrate how you can alter SharePoint 2013 search results based upon the current user.  The example I’ll use here is promoting specific search results to the top of the search result list based on the user’s department in Active Directory.  This feature is known as User Segmentation and the method I describe is my take on the information and code available here http://blogs.technet.com/b/speschka/archive/2012/12/02/using-user-context-aka-as-segmentation-in-search-with-sharepoint-2013.aspx and here http://msdn.microsoft.com/en-us/library/jj870831.aspx

In this post I’ll show the user properties within SharePoint 2013 for two test users.  Each user has a different department and will see the search results ordered differently.

UCSS1 UCSS2

Once the users have been imported from Active Directory, the properties are displayed in the User Profile Service Application.

UCSS3 UCSS4

During the user import process a taxonomy is automatically created to hold the department values.  This is where the web part looks up the user’s department and retrieves the term tag ID.  You can see a term has been created for each department.  You can open the Managed Metadata Service from Site Settings, or from the Service Application.

UCSS5

Add the custom web part to a SharePoint web part page.  The link to download the Visual Studio solution and installation instructions are in the previous post.

UCSS6

Edit the web part properties and click on Change query.  On the basics tab select your search source and enter your search term.  Click Test to run the query, results will appear in the right-hand pane.  Click OK to save changes.UCSS7

On the web part properties ensure that result table is set to RelevantResults.  Save you changes to the page.

UCSS8

At this point the web part is returning same results as the standard Content Search Web Part.  The next step is to create query rules to alter the search results based on the user’s department.

Navigate to your Search Service Application and open Query Rules.

UCSS10

Select Local SharePoint Results and click New Query Rule.  This will create a query rule that will fire for any search of local SharePoint content.

UCSS11

Enter a rule name, expand Context, then under “Query is performed by these user segments” select “One of these user segments” and click Add User Segment.  Here we will select the department for which we want the rule to fire.

UCSS12Enter a title for the segment, e.g. IT Services and click add user segment term.  Expand the Managed Metadata Service, then navigate to the Department taxonomy and select the desired department, click Save.

UCSS13The query rule should now look as follows.

UCSS14Under Query Conditions click Remove Condition as we want the query rule to fire for any search term.

UCSS15In the Actions section click Change ranked results by changing the query.  Here we will promote results with a certain keyword in their title.

UCSS16In order to experiment with the query rule you need to have enabled the Search Server Web Parts and Templates feature under Site Settings, Site Collection Features in the Central Administration web site.  If this feature hasn’t already been enabled, you will receive an error when testing the query.  If this happens, cancel the Build Your Query wizard, enable the feature and return here to continue the process.

When the Build Your Query wizard loads enter a search term in the Query text box and click Test query.  The results in the search result preview pane show the search results before we begin to alter the ranking.

UCSS17Next, click onto the Sorting tab.  Click Set top sorting level to rank, then click Add dynamic ordering rule.

UCSS18In this example I’m going to promote search results with the word “health” in the title to the top of the search results.

UCSS19Click onto the Test tab to preview the changes the dynamic ordering rule has made to the search result ranking.  You can see the result with “health” in the title has been promoted to the top of the search results.

UCSS20Remove your test query term from the Basics tab, click Ok, followed by Save to save your query rule.  For this example I repeated the process to create a second rule.  This rule fires for users in the Finance department and promotes results with the keyword “tax” to the top of the search results.

Below are three screenshots.  Each screenshot shows the same SharePoint page with the customised web part at the top and a standard Content Search Web Part at the bottom viewed as a different user.  You can see that the query rule using user segmentation only works on the custom web part.  The first screenshot is for a user that’s not in IT Services, or Finance and you can see the search results for both web parts are the same.  The second screenshot is for a user in IT Services and you can see the results with “health” in the title have been promoted.  The final screenshot is for a user in the Finance department and you can see the results with “tax” in the title have been promoted.

UCSS21UCSS22

UCSS23

The process I have described in this two part series is just an example.  You can adapt the web part and query rules to meet you own requirements.  Query rules are extremely powerful, yet easy to use.  I hope this two part series has give you some ideas you can implement in your environment.

Advertisements

5 thoughts on “User Context Sensitive Searching In SharePoint 2013 Part 2

  1. Pingback: SharePoint 2013 : Recherche – Présentation du contexte utilisateur (User Segmentation) | SharePoint by Benoît Jester (Asipe)

  2. Vladimir

    Thanks for explain, Richard.
    I have tryed to make it by Steve’s post and not successfully. Now it works.

    Reply
    1. Vladimir

      Richard, excuse me, but I have a problem – my query rules is active but not fired for web part when I pressed “Change query” for web part and removed my tag (department) from query on Basic tab. Query result is empty.
      I don’t know what to do.

      Reply
      1. richardstk Post author

        Are you using SharePoint Enterprise? This won’t work with SharePoint standard. Also, try starting with a new copy of the web part and start to build your query again, gradually adding filters and seeing their effect on the results.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s