SBX - Search With Button

SBX - Forum Post Title

Extending the Faceted Search - Results Template" in Portal 8.2

Microsoft Dynamics CRM Forum

AndrewV asked a question on 26 Jan 2017 12:04 PM
My Badges

Question Status

Verified

The "Faceted Search - Results Template" currently uses this template to show data:

    {{#each items}}
     <li>
      <h3><a title="{{title}}" href="{{url}}">{{title}}</a></h3>
      <p class="fragment">{{{fragment}}}</p>
      <div>
       {{#each tags}}
        <span class="{{cssClass}}">{{./label}}</span>
       {{/each}}
      </div>
     </li>
    {{/each}}

I'd like to be able to show other fields from the related item in the display. I worked backwards through the context that the handlebar template was using and found this data at the item level:

    entityID: 9262a5d1-7dd7-e611-80fc-5065f38b4251 
    entityLogicalName: knowledgearticle 
    title: First Article 
    fragment: &hellip;&nbsp;<em class="highlight">First</em> Article&nbsp;&hellip; 
    resultNumber: 1 
    score: 1.8315227 
    url: /knowledgebase/article/KA-01000 
    absoluteUrl: ...microsoftcrmportals.com/.../KA-01000 
    tags: 
        entityLogicalNames: knowledgearticle 
        cssClass: label label-primary 
        label: Knowledge Base

I have two questions as a result:

1) How do I include additional fields from my articles to display in the handlebar template?

2) What is the "tags' array, and where can I modify/see it? I can pull up the "Tag" entity, but the data here doesn't correspond to any record I can find in the system.

Thanks!

Reply
Colin Vermander responded on 26 Jan 2017 3:41 PM
My Badges
Suggested Answer

Hi Andrew, you would need to query using liquid to look up the full entity.  The result item will have the limited attributes you have already found.  With the result item it has entityID and entityLogicalName so you should be able to easily do that additional lookup to access your extra fields.

See the following as well you can find similar documentation in the administrators guide:

community.adxstudio.com/.../entities

The tags array is just the record types.  So if the result was a member of multiple types it would be in that array.  Each type has its own logical name and css class.

Colin

Reply
AndrewV responded on 27 Jan 2017 7:27 AM
My Badges

Thanks for the response Colin.

I'm having problems visualizing (and writing) liquid that would retrieve that data. The context from the handlebar template needs to be within {%raw%} tags and appears to be resolved after the liquid templates are resolved. I would need the tags within {%raw%} to be resolved first for the liquid template to have the correct ID to query against.

Any suggestions?

As an example, this doesn't work because 1) the raw tags are resolved after liquid, and 2) you don't appear to be able to nest tags:

    {%endraw%}{% assign kba = entities.knowledgearticle["{%raw%}{{entityID}}{%endraw%}"]%}

     {% if kba %}

       {{ kba.title }} {{{ kba.statecode.label }}}

     {% endif %}{%raw%}

Reply
AndrewV responded on 27 Jan 2017 7:35 AM
My Badges

What I've also considered is using liquid to generate a basic json object with the bare minimum number of fields I am looking for. Then I can parse through it with JavaScript and add additional content to the page. The only problem is I can't do a liquid fetch for exactly what the query is returning (that data is only in the handlebar context), so I'd need to pull a lot of additional content that "might" be on the page.

Reply
Colin Vermander responded on 27 Jan 2017 7:03 PM
My Badges
Verified Answer

Well that didn't really pane out how I thought.  Seems like querying different records in that handlebars doesn't work, or at least I have not found a way to do it.  Doing entities lookup on each item with the AJAX seems to be fail, if you hard code the liquid it does work though so its something with the handlebars that is stopping this.  I tried moving the liquid logic to another web template and still had the same issue.

Here is another trick which should allow you to do it though, its not the greatest probably for performance as it is resulting in a lot of requests, an extra for each result.  So since this is handlebars we can register a helper that will then make an jquery ajax call which will call a web template with your liquid logic to get the data you want.  There might be a better way to this, everyone feel free to improve.

Let's start with creating a new web template that has the logic of what you want to add to each result.  My template is fairly simple, you could add additional parameters like logical name as well.  You could put whatever logic you wanted in here, have like a switch case for the different result types, etc.

{% assign eid = request.params['entityid'] %}
{% assign wpage = entities.adx_webpage[eid] %}

<p><strong>Name:</strong> {{wpage.adx_name}}</p> 

Once created, now create a page template that uses this web template, ensure that you untick the "Use Website Header and Footer" as all we want out of this is the HTML you want to add to each result.  Now add a web page that uses this page template.  You can test it by hitting the URL manually, ie. /search-details/?entityid=94216ad7-7864-e611-80d7-00155db4fa48, I just pulled some random page ID.

Now we need to register our handlebars helper.  Go to the search page in the site (run a search), make sure you are logged in with an account that as front-side editor (or you can add this via CRM).  On the search page select the Edit from the editor control, go to the Options tab and add the following to the custom JavaScript.

Handlebars.registerHelper("search_details", function (obj) {
    return $.ajax({
      url: "/search-details/?entityid=" + obj.fn(this),
      type: "GET",
      async: false
    }).responseText;
});

You will need to modify the URL for your web page that you setup.

Now head over to the web template for the results rendering - "Faceted Search - Results Template".  Within the {{#each items}} where you want the details located add

{{#search_details}}{{entityID}}{{/search_details}}

Here is what my complete each looked like:

     {{#each items}}
     <li>
      <h3><a title="{{title}}" href="{{url}}">{{title}}</a></h3>
      <p class="fragment">{{{fragment}}}</p>
      {{#search_details}}{{entityID}}{{/search_details}}
      <div>
       {{#each tags}}
        <span class="{{cssClass}}">{{./label}}</span>
       {{/each}}
      </div>
     </li>
    {{/each}}


Save, refresh search result and there should be your extra data as part of each result!

Remember to give entity permissions on the entities you query with liquid.

The thing that is really not great about this is the fact that an extra request will be made for each result.  You can debug this and see the extra requests in the browser tools network tab.  I will still look for a better way to do this but this is currently what I have working.

Reply
AndrewV responded on 27 Jan 2017 9:00 PM
My Badges

Great answer. Thank you for the insight Colin!

Reply
Colin Vermander responded on 27 Jan 2017 7:03 PM
My Badges
Verified Answer

Well that didn't really pane out how I thought.  Seems like querying different records in that handlebars doesn't work, or at least I have not found a way to do it.  Doing entities lookup on each item with the AJAX seems to be fail, if you hard code the liquid it does work though so its something with the handlebars that is stopping this.  I tried moving the liquid logic to another web template and still had the same issue.

Here is another trick which should allow you to do it though, its not the greatest probably for performance as it is resulting in a lot of requests, an extra for each result.  So since this is handlebars we can register a helper that will then make an jquery ajax call which will call a web template with your liquid logic to get the data you want.  There might be a better way to this, everyone feel free to improve.

Let's start with creating a new web template that has the logic of what you want to add to each result.  My template is fairly simple, you could add additional parameters like logical name as well.  You could put whatever logic you wanted in here, have like a switch case for the different result types, etc.

{% assign eid = request.params['entityid'] %}
{% assign wpage = entities.adx_webpage[eid] %}

<p><strong>Name:</strong> {{wpage.adx_name}}</p> 

Once created, now create a page template that uses this web template, ensure that you untick the "Use Website Header and Footer" as all we want out of this is the HTML you want to add to each result.  Now add a web page that uses this page template.  You can test it by hitting the URL manually, ie. /search-details/?entityid=94216ad7-7864-e611-80d7-00155db4fa48, I just pulled some random page ID.

Now we need to register our handlebars helper.  Go to the search page in the site (run a search), make sure you are logged in with an account that as front-side editor (or you can add this via CRM).  On the search page select the Edit from the editor control, go to the Options tab and add the following to the custom JavaScript.

Handlebars.registerHelper("search_details", function (obj) {
    return $.ajax({
      url: "/search-details/?entityid=" + obj.fn(this),
      type: "GET",
      async: false
    }).responseText;
});

You will need to modify the URL for your web page that you setup.

Now head over to the web template for the results rendering - "Faceted Search - Results Template".  Within the {{#each items}} where you want the details located add

{{#search_details}}{{entityID}}{{/search_details}}

Here is what my complete each looked like:

     {{#each items}}
     <li>
      <h3><a title="{{title}}" href="{{url}}">{{title}}</a></h3>
      <p class="fragment">{{{fragment}}}</p>
      {{#search_details}}{{entityID}}{{/search_details}}
      <div>
       {{#each tags}}
        <span class="{{cssClass}}">{{./label}}</span>
       {{/each}}
      </div>
     </li>
    {{/each}}


Save, refresh search result and there should be your extra data as part of each result!

Remember to give entity permissions on the entities you query with liquid.

The thing that is really not great about this is the fact that an extra request will be made for each result.  You can debug this and see the extra requests in the browser tools network tab.  I will still look for a better way to do this but this is currently what I have working.

Reply
Colin Vermander responded on 26 Jan 2017 3:41 PM
My Badges
Suggested Answer

Hi Andrew, you would need to query using liquid to look up the full entity.  The result item will have the limited attributes you have already found.  With the result item it has entityID and entityLogicalName so you should be able to easily do that additional lookup to access your extra fields.

See the following as well you can find similar documentation in the administrators guide:

community.adxstudio.com/.../entities

The tags array is just the record types.  So if the result was a member of multiple types it would be in that array.  Each type has its own logical name and css class.

Colin

Reply

SBX - Two Col Forum

SBX - Migrated JS