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.
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 ";
}
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.