2

TL;DR

Is there a way to have some kind of event or way to know when a Catalog item resuse webpart doesn't return an item to the control template?

Here are all the details of what I have tried and the places I have looked

We have a cross-site publishing 2013 on-prem site. The catalog is setup and everything is working. However if a user goes to a catalog item page for an item that doesn't exist the page breaks. I would like to display a The item you are looking for doesn't seem to be here. Please browse our catalog or try your search again. kind of message.

The problem seems to be with the catalog item reuse snippet which handles display getting the item information to the page. It is described in this blog and a possible solution to extend the catalog item reuse webpart is provided. However we can't create new webparts. So I'm looking for a solution within the client-side site-owner type of solutions.

First I looked at the snippet on my page layout. I had set it up to use custom Control and Item templates. But other than that I don't see any attributes in there that would allow me to handle when no items are returned. I do find it odd that the NumberOfItems is hard coded at 1, what is that about?

<!--SPM:<a2e8ead9d:CatalogItemReuseWebPart
    DataProviderJSON="{QueryTemplate stuff in here}"
    RenderTemplateId="~sitecollection/_catalogs/masterpage/Display Templates/Control_Project1.js"
    ItemTemplateId="~sitecollection/_catalogs/masterpage/Display Templates/Item_Project1.js"
    runat="server"
    UseServerSideRenderFormat="False"
    ResultType=""
    NumberOfItems="1"
    UseSharedDataProvider="False"
    OverwriteResultPath="False"
    ResultsPerPage="1"
    SelectedPropertiesJson="[&#34;Title&#34;]"
    Title=""
    Description=""
    MissingAssembly="Cannot import this Web Part."
    ID="g_8579ad6a_9991_40db_a92d_6ca6cdc7364c"
    __WebPartId="{8579ad6a-9991-40db-a92d-6ca6cdc7364c}"
    >-->

The template js files are autogenerated from the associated html files that I can edit and upload.

I looked into my Item_Project1.js template. But I noticed that on pages without associated items the item template file is is never loaded. So I can't put anything in that file to deal with the issue.

However the Control_Project1.js template is loaded even on an empty page. I looked at other control templates in my site and found the following bits of code:

var $noResults = Srch.ContentBySearch.getControlTemplateEncodedNoResultsMessage(ctx.ClientControl);
...
<!--#_     
if (ctx.ClientControl.get_shouldShowNoResultMessage()){
_#-->
        <div class="_#= noResultsClassName =#_">_#= $noResults =#_</div>
<!--#_
}
_#-->

I also tried adding a test in the code like

if(typeof ctx.ListData.ResultTables[0].ResultRows === 'undefined'){
   console.log("No data for you!")
}

But neither of those pieces of code every run. Using the debugger I see that the code I enter is wrapped in a function like function DisplayTemplate_2d1c504c7e6941ff993e412cd306d1ad(ctx)() and that further down there is some code to register that function as a callback for something.

I've also used the debugger to track the calls and events and it seems that in search.cbs.js there is some code like this which calls displayControlMessage if there is no items and calls processResultReady otherwise.

processResultReady: function(b) {
    var c = this.$9_5(b);
    if (!this.shouldRenderControl() && this.$1_5) {
        this.processDataProviderErrors(this.get_dataProvider());
        if (!this.hasMessages()) {
            var d = new Srch.ControlMessage(null ,0,-1,"HiddenWithNoResultsWarningMessage",Srch.U.loadResource("cc_err_HiddenWithNoResultsWarningMessage"),"","",null ,true,false,true)
              , a = [];
            Srch.U.appendArray(a, d);
            this.processDataErrorMessages(a)
        }
        this.displayControlMessages();
        this.setControlElementVisibility(false)
    } else
        Srch.Result.prototype.processResultReady.call(this, c)
}

But this is really getting into the SP weeds and I'm out of my depth.

Your Answer

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

Browse other questions tagged or ask your own question.