We found a solution allowing us to manipulate the Content Search web part in the following ways:
1) Append a "Show all" link to the web part's visual output
2) Add custom properties and use the values in code-behind.
First, we created a new (non-visual) web part extending ContentBySearchWebPart. Then we overrode CreateChildControls, adding our link control:
protected override void CreateChildControls()
{
if (!string.IsNullOrEmpty(ShowAllUrl) && !string.IsNullOrEmpty(ShowAllText))
{
Link link = new Link();
link.NavigateUrl = ShowAllUrl;
link.Text = ShowAllText;
link.CssClass = "ShowAll_Link";
Controls.Add(link);
}
base.CreateChildControls();
}
Next, we add custom properties. First we declare our properties:
[System.Web.UI.WebControls.WebParts.WebBrowsable(true),
System.Web.UI.WebControls.WebParts.WebDisplayName("Display text"),
System.Web.UI.WebControls.WebParts.WebDescription(""),
System.Web.UI.WebControls.WebParts.Personalizable(
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
System.ComponentModel.Category("Link to Show All"),
System.ComponentModel.DefaultValue("Show all")]
public string ShowAllText { get; set; }
[System.Web.UI.WebControls.WebParts.WebBrowsable(true),
System.Web.UI.WebControls.WebParts.WebDisplayName("URL"),
System.Web.UI.WebControls.WebParts.WebDescription(""),
System.Web.UI.WebControls.WebParts.Personalizable(
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
System.ComponentModel.Category("Link to Show All"),
System.ComponentModel.DefaultValue("")]
public string ShowAllUrl { get; set; }
Finally, we must override GetToolParts() in order to inject our custom properties into the settings panel:
public override ToolPart[] GetToolParts()
{
List<ToolPart> tp = new List<ToolPart>(base.GetToolParts());
tp.Add(new CustomPropertyToolPart());
return tp.ToArray();
}
That's all there is to it. Here is the full class:
using Microsoft.Office.Server.Search.WebControls;
using Microsoft.SharePoint.Mobile.Controls;
using Microsoft.SharePoint.WebPartPages;
namespace Netcompany.FVS0001
{
using System.Collections.Generic;
public class CustomContentSearchWebPart : ContentBySearchWebPart
{
[System.Web.UI.WebControls.WebParts.WebBrowsable(true),
System.Web.UI.WebControls.WebParts.WebDisplayName("Display text"),
System.Web.UI.WebControls.WebParts.WebDescription(""),
System.Web.UI.WebControls.WebParts.Personalizable(
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
System.ComponentModel.Category("Link to Show All"),
System.ComponentModel.DefaultValue("Show all")]
public string ShowAllText { get; set; }
[System.Web.UI.WebControls.WebParts.WebBrowsable(true),
System.Web.UI.WebControls.WebParts.WebDisplayName("URL"),
System.Web.UI.WebControls.WebParts.WebDescription(""),
System.Web.UI.WebControls.WebParts.Personalizable(
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
System.ComponentModel.Category("Link to Show All"),
System.ComponentModel.DefaultValue("")]
public string ShowAllUrl { get; set; }
/// <summary>
/// Override the creation of the settings panel, telling it to add our own custom properties from this class
/// </summary>
/// <returns></returns>
public override ToolPart[] GetToolParts()
{
List<ToolPart> tp = new List<ToolPart>(base.GetToolParts());
tp.Add(new CustomPropertyToolPart());
return tp.ToArray();
}
/// <summary>
/// Add a "Show all" link
/// </summary>
protected override void CreateChildControls()
{
if (!string.IsNullOrEmpty(ShowAllUrl) && !string.IsNullOrEmpty(ShowAllText))
{
Link link = new Link();
link.NavigateUrl = ShowAllUrl;
link.Text = ShowAllText;
link.CssClass = "ShowAll_Link";
Controls.Add(link);
}
base.CreateChildControls();
}
}
}