I have created a web part programmatically (C#) to show SharePoint 2013 search results by extending class ResultScriptWebPart .
But I couldn't set Display template property by the code. Is there any possible way to set the display template by the code???
Here is my code:
protected override void OnLoad(EventArgs e){
//Invisible the below fields from search results
this.ShowPaging = true;//Show the paging in the search resluts.
this.ShowResultCount = true;//Display the count of the results
this.ShowPreferencesLink = false;//Not to display preferences link
this.ShowAdvancedLink = false;//Not to display advanced search link
this.ShowAlertMe = false;//Not to display alert me
//Set the number results per page
this.ResultsPerPage = 3;
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);
}
private void EnhanceQuery(object sender, BeforeSerializeToClientEventArgs e)
{
DataProviderScriptWebPart dataProvider = sender as DataProviderScriptWebPart;
string query = string.Format("(contentclass:STS_ListItem_DocumentLibrary) AND -FileExtension=\"aspx\""); //Set your actuall query here.
dataProvider.QueryTemplate = query;
SetResultDisplay();