3

Using SharePoint 2013 I have a Content Search web part that I'm using to display 5 News Articles using the following display templates:

  • Control: List
  • Item: Picture on Left, 3 lines on right

And this is working just fine.

What I would like to include is a link after the 5 News Articles that will take the user to a "News Archive" page where it displays all the News Articles.

I'm thinking of creating a custom Control display template based off the OOTB Control_List.js display template. I suppose I could hard code the link to the News Archive in the display template, but I would like to make it reusable. Is there a way I could dynamically pass data to the Control display template?

Any other suggestions on how to approach this?

8

You should inherit ContentBySearchWebPart, and overload the OnLoad event:

protected override void OnLoad(EventArgs e)
{
    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(EnhanceQuery);

         }
     }

     base.OnLoad(e);
}


protected virtual void EnhanceQuery(object sender, BeforeSerializeToClientEventArgs e)
{
    DataProviderScriptWebPart dataProvider = sender as DataProviderScriptWebPart;
    dataProvider.Properties.Add("MyProperty", this.MyProperty);
}

Now you can get this property in display template HTML:

<!--#_
    var myValue = ctx.DataProvider.get_properties()["MyProperty"];
_#-->
<div>_#= myValue =#_</div>
  • Thanks for the Post. I've tried above code. But i could not see the property. could u please guide me where i need to check the property ? Whether in tool pane or search query pane? – user31424 Jul 10 '14 at 11:39
  • You can see the added property in the display template. – Shosh Feb 5 '15 at 9:11
0

I am wondering why you need display template for this at all! You can put a limit of 5 number of result in CSWP and Add a Content Editor web part a the end of page (After CSWP) to guide user to new page.

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.