0

I want to use the Content Search web part to display a site's latest news articles. Also, I need a "Show all" link to the bottom of the web part - this link would take the user to a search results page. The link text and URL must be customizable in the Web part properties.

What is the best way to go about this? I have found examples of extending the CSWP for the purpose of programmatically manipulating the search query, but I haven't found examples of how to change the output.

0

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();
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.