Click here to Skip to main content
Click here to Skip to main content

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

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:
 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);
  }
}
base.OnLoad(e);
  • 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.

License

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

Share

About the Author

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

Comments and Discussions

 
QuestionCustom webpart does not display any result on page Pinmemberpooja 083-Sep-14 1:00 

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

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

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