Extend ResultScriptWebPart to display the search results

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();
       
January 6th, 2014 1:07pm

Hi,
For this issue, I'm trying to involve someone familiar with this topic to further look at it.
Thanks,

Free Windows Admin Tool Kit Click here and download it now
January 13th, 2014 1:51am

Hi Suhail Jamaldeen,

as i know the display template is part of the javascript, so you may check with different of browsers also, and check for the result, if the result is not quite right.

for the example to join the display template and the resultItemType, you may check this 3rd party example:

http://dev.net.ua/blogs/ivanbilokon/archive/2012/11/22/11389.aspx

public static ResultItemType CreateResultType(SPWeb web, string name, string displayTemplateUrl, PropertyRule[] rules, bool optimizeForFrequentUse)
        {
            ResultItemType resType = new ResultItemType(new SearchObjectOwner(SearchObjectLevel.SPWeb, web));
            resType.Name = name;
            resType.SourceID = new Guid();
            resType.DisplayTemplateUrl = "~sitecollection" + displayTemplateUrl;
            SPFile file = web.GetFile(SPUtility.ConcatUrls(web.ServerRelativeUrl, displayTemplateUrl));
            resType.DisplayProperties = ParseManagePropertyMappings(file.ListItemAllFields["Managed Property Mappings"].ToString());
            resType.Rules = new PropertyRuleCollection(new List<PropertyRule>(rules));
            typeof(ResultItemType).GetProperty("OptimizeForFrequentUse").SetValue(resType, optimizeForFrequentUse);
            return resType;
        }

and another example http://blogs.technet.com/b/speschka/archive/2012/07/23/using-query-rules-result-types-and-display-templates-for-a-custom-search-sales-report-in-sharepoint-2013.aspx


January 13th, 2014 4:30am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics