Click here to Skip to main content
15,883,531 members
Articles / Productivity Apps and Services / Sharepoint

Extend Content Search web part to add Target Audience filter

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
24 Jul 2014CPOL1 min read 23.5K   3   5
This article explains about adding Target Audience feature to OOB Content Search web part


The most frequent requirement that we usually get is displaying items based on the Target Audience values. SharePoint 2013 have a very powerful web part called Content Search web part that displays content from the search content source.   All of us are aware that SharePoint Search web parts displays the contetnt based on item permissions. 

This article explains about how to use Content Search web part to display the crawled content based on Target Audience.


List "Products" with columns "Title", "Description" and "TargetAudience". "TargetAudience" column is of type 'People and Group' and this example covers the Group values for TargetAudience.


Bydefault when using Content Search web part, this list content is visible to all users irrespeective of the group they belongs to. This example explains how to restrict the content to those groups specified under "TargetAudience".

Using the code

This is not possible with OOB Content Search and we need to extend the Content Search web part to add the new functionality.

  • Create new empty project in Visual Studio.
  • Add New web part item to the project.
  • Add following references:
using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.WebControls;
  • Inherit class from ContentBySearchWebPart.
public class CustomCSWP : ContentBySearchWebPart
  • Add following code in OnLoad event:
if (this.AppManager != null)
  if (this.AppManager.QueryGroups.ContainsKey(this.QueryGroupName) &&
   this.AppManager.QueryGroups[this.QueryGroupName].DataProvider != null)
    this.AppManager.QueryGroups[this.QueryGroupName].DataProvider.BeforeSerializeToClient +=
                        new BeforeSerializeToClientEventHandler(UpdateQueryText);
  • Add following code to the web part:
private void UpdateQueryText(object sender, BeforeSerializeToClientEventArgs e)
    DataProviderScriptWebPart dataProvider = sender as DataProviderScriptWebPart;
    string currentQueryText = dataProvider.QueryTemplate;
    string token = "TargetAudienceQuery";
    if (currentQueryText.Contains(token))
        dataProvider.QueryTemplate = currentQueryText.Replace(token, BuildTAQuery());
private string BuildTAQuery()
    string query = string.Empty;
    SPSite site = new SPSite(SPContext.Current.Web.Url);
    SPWeb web = site.OpenWeb();
    SPUser currentUser = web.CurrentUser;
    SPGroupCollection grpColl = currentUser.Groups;
    if (grpColl.Count > 0)
        foreach (SPGroup grp in grpColl)
            query += "TargetAudienceOWSUSER:" + grp.Name + " OR "; // TargetAudienceOWSUSER is the managed property ganerated for Target Audience column
        query = query.Substring(0, query.LastIndexOf(" OR "));
    return query;
  • Build and deploy this web part.
  • Go to SharePoint site and add this web part on a page. 
  • Add following query and make sure it fetches "Products" items. CSWP
  • Add TargetAudienceQuery at the end of this query and click OK. Save and Publish the page. 

Final query is like below:

(IsDocument:"True" OR contentclass:"STS_ListItem")  path:"<Server Name>/lists/Products"  TargetAudienceQuery

When published TargetAudienceQuery will be replaced by the query that we built in web part.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Capgemini US LLC
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionFiltering Search Results to Eliminate Matches on Web Parts Target Audience and the user is not a member of that Audience Pin
Steve Mannina26-Feb-16 0:38
Steve Mannina26-Feb-16 0:38 
QuestionNot working Pin
Mekhlouf Salah Eddine4-Apr-15 4:59
Mekhlouf Salah Eddine4-Apr-15 4:59 
BugProblem with whitespaces Pin
Adrinanet18-Feb-15 5:51
Adrinanet18-Feb-15 5:51 
QuestionAdded Query lost at paging in anonymous mode Pin
Rivki1234-Feb-15 22:02
Rivki1234-Feb-15 22:02 
QuestionCustom webpart does not display any result on page Pin
pooja 083-Sep-14 1:00
pooja 083-Sep-14 1:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.