Click here to Skip to main content
12,815,937 members (37,256 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 22 Jul 2014

Extend Content Search web part to add Target Audience filter

, 24 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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.
  • 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)


About the Author

Srikanth Tiyyaguru
Capgemini US LLC
United States United States
No Biography provided

You may also be interested in...

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
Member 1158835126-Feb-16 1:38
memberMember 1158835126-Feb-16 1:38 
QuestionNot working Pin
Mekhlouf Salah Eddine4-Apr-15 5:59
memberMekhlouf Salah Eddine4-Apr-15 5:59 
BugProblem with whitespaces Pin
Adrinanet18-Feb-15 6:51
memberAdrinanet18-Feb-15 6:51 
QuestionAdded Query lost at paging in anonymous mode Pin
Rivki1234-Feb-15 23:02
memberRivki1234-Feb-15 23:02 
QuestionCustom webpart does not display any result on page Pin
pooja 083-Sep-14 2:00
memberpooja 083-Sep-14 2: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 24 Jul 2014
Article Copyright 2014 by Srikanth Tiyyaguru
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid