I had created a custom ContentBySearchWepart (CSWP), by extending the original Webpart, so that i could customize the query at runtime. Although I had problems initially, but resolved to some extent via : This Question
The main part was to change DataProviderJson Property of the Webpart. Below is an example..
string dataProviderJson = "{{'Properties':{{'TryCache':false,'Scope':'{0}','HHTitle_WB_Marker1':50,'HHTitle_WB_Marker2':60,'UpdateLinksForCatalogItems':true,'EnableStacking':true}},'QueryGroupName':'Default','SourceID':'8413cd39-2156-4e00-b54d-11efd9abdb89','SourceName':'Local SharePoint Results (System)','SourceLevel':'Ssa','CollapseSpecification':'','QueryTemplate':'{{searchTerms?}} (contentclass:sts_listitem OR IsDocument:True) SPSiteUrl:{1} ListId:{2} {{?owstaxidMetadataRegion:{3}}}','FallbackSort':[{{'p':'Rank','d':0}}],'RankRules':[],'AsynchronousResultRetrieval':false,'SendContentBeforeQuery':false,'BatchClientQuery':false,'FallbackLanguage':-1,'FallbackRankingModelID':'','EnableStemming':true,'EnablePhonetic':false,'EnableNicknames':false,'EnableInterleaving':false,'EnableQueryRules':true,'EnableOrderingHitHighlightedProperty':false,'HitHighlightedMultivaluePropertyLimit':-1,'IgnoreContextualScope':true,'ScopeResultsToCurrentSite':false,'TrimDuplicates':false,'ClientType':'ContentSearchRegular','UpdateAjaxNavigate':true,'SummaryLength':180,'DesiredSnippetLength':90,'PersonalizedQuery':false,'FallbackRefinementFilters':null,'IgnoreStaleServerQuery':false,'RenderTemplateId':'DefaultDataProvider','AlternateErrorMessage':null,'Title':''}}"; dataProviderJson = String.Format(dataProviderJson, siteUrl, siteUrl + "/sites/Catalog", catalogListID, String.IsNullOrEmpty(culture) ? "en-US" : culture);
I created 2-3 webparts like this and added on a page.
- The First Problem that i saw was that, all webparts added after the first webpart are not working right. The query was generating correctly in code behind, but the result displayed on the page was according to the default query that was generated when the webpart was added first.
The fact with CSWP is that, it generates the result on client side and rendering is also on client side.
I could also get to a solution if somebody can explain me the lifecycle on CSWP correctly.