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.
Once the users have been imported from Active Directory, the properties are displayed in the User Profile Service Application.
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.
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.
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.
On the web part properties ensure that result table is set to RelevantResults. Save you changes to the page.
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.
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.
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.
Enter 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.
The query rule should now look as follows.
Under Query Conditions click Remove Condition as we want the query rule to fire for any search term.
In the Actions section click Change ranked results by changing the query. Here we will promote results with a certain keyword in their title.
In 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.
Next, click onto the Sorting tab. Click Set top sorting level to rank, then click Add dynamic ordering rule.
In this example I’m going to promote search results with the word “health” in the title to the top of the search results.
Click 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.
Remove 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.
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.