Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / SharePoint

Extend Content Search web part to add Target Audience filter

5.00/5 (2 votes)
24 Jul 2014CPOL1 min read 24.1K  
This article explains about adding Target Audience feature to OOB Content Search web part

Introduction

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.

Background

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.

Products

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:
C#
using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.WebControls;
  • Inherit class from ContentBySearchWebPart.
C#
public class CustomCSWP : ContentBySearchWebPart
  • Add following code in OnLoad event:
C#
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);
  }
}
base.OnLoad(e);
  • Add following code to the web part:
C#
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());
    }
}
C#
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.

License

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