Forum Discussion

skylinegtr's avatar
10 years ago

Need help with calling two API

How can I call two API in one custom component box? I want to display posts from the Forums and Knowledge Base. My code is below. Please help.

 

<li style="height: 342px;" class="tile">
<div class="border">
<h2 class="tile-title">Freatured Post</h2></div>
<div class="inner">
<#attempt>
<#assign messages = rest("/boards/id/knowledge/topics/recent?page_size=4").messages />
<#recover>
</#attempt>
<div class="custom-recent-articles-container">
<#if messages.message?size gt 0>
<div class="custom-recent-articles-heading-bar">
<span class="custom-recent-articles-heading-bar-title">${text.format("")}</span>
</div>
</#if>
<#list messages.message as message>
<#assign posted_by_userid = message.author.login?string />
<#assign posted_by_label = text.format("Posted By:") />
<div class="custom-recent-article-wrapper">
<div class="custom-article-header">
<div class="custom-article-subject">
<a href="${message.@view_href}">${message.subject}</a>
</div>
<div class="custom-article-posted-by">
<span class="author">${posted_by_label} <#if ((posted_by_userid != "") && (posted_by_userid != "Anonymous"))><@component id="common.widget.user-name" user="conv:${message.author.login}" /></#if></span>
</div>
</div>
<div class="custom-article-teaser">
<#if message.teaser?length gt 0>
${message.teaser}
<span class="custom-read-more"><a href="${message.@view_href}">${text.format("")}</a></span>
</#if>
</div>
</div>
</#list>
</div>
</div>
</li>

  • skylinegtr try the code below but in the SELECT statement replace 'travellers' and 'ULTRA board Knowledge base' with your board and knowledgebase

     

    <li style="height: 342px;" class="tile">
      <div class="border"><h2 class="tile-title">Freatured Post</h2></div>
      <div class="inner">  
        <#attempt>
          <#assign messages = rest("2.0","/search?q=" + "SELECT subject, view_href, post_time_friendly, board.id, teaser, author  FROM messages WHERE board.id IN('travellers', 'ULTRA board Knowledge Base') ORDER BY post_time DESC LIMIT 10"?url) />
          <#recover>
        </#attempt>
        
        <#if messages.message?size gt 0>
          <div class="custom-recent-articles-container">
            <div class="custom-recent-articles-heading-bar"><span class="custom-recent-articles-heading-bar-title">${text.format("")}</span></div>    
    
            <#list messages.data.items as message > 
              <#assign posted_by_userid = message.author.login?string />
              <#assign posted_by_label = text.format("Posted By:") />
        
              <div class="custom-recent-article-wrapper">
                <div class="custom-article-header">
                  <div class="custom-article-subject"><a href="${message.view_href}">${message.subject}</a></div>
                  <div class="custom-article-posted-by">
                    <span class="author">${posted_by_label} <#if ((posted_by_userid != "") && (posted_by_userid != "Anonymous"))><@component id="common.widget.user-name" user="conv:${message.author.login}" /></#if></span>
                  </div>
                </div>
                <div class="custom-article-teaser">
                  <#if message.teaser?length gt 0>
                    ${message.teaser} <span class="custom-read-more"><a href="${message.@view_href}">${text.format("")}</a></span>
                  </#if>
                </div>
              </div>
            </#list>
          </#if>
        </div>
      </div>
    </li>
    
  • OlivierS's avatar
    OlivierS
    Lithium Alumni (Retired)

    skylinegtr try the code below but in the SELECT statement replace 'travellers' and 'ULTRA board Knowledge base' with your board and knowledgebase

     

    <li style="height: 342px;" class="tile">
      <div class="border"><h2 class="tile-title">Freatured Post</h2></div>
      <div class="inner">  
        <#attempt>
          <#assign messages = rest("2.0","/search?q=" + "SELECT subject, view_href, post_time_friendly, board.id, teaser, author  FROM messages WHERE board.id IN('travellers', 'ULTRA board Knowledge Base') ORDER BY post_time DESC LIMIT 10"?url) />
          <#recover>
        </#attempt>
        
        <#if messages.message?size gt 0>
          <div class="custom-recent-articles-container">
            <div class="custom-recent-articles-heading-bar"><span class="custom-recent-articles-heading-bar-title">${text.format("")}</span></div>    
    
            <#list messages.data.items as message > 
              <#assign posted_by_userid = message.author.login?string />
              <#assign posted_by_label = text.format("Posted By:") />
        
              <div class="custom-recent-article-wrapper">
                <div class="custom-article-header">
                  <div class="custom-article-subject"><a href="${message.view_href}">${message.subject}</a></div>
                  <div class="custom-article-posted-by">
                    <span class="author">${posted_by_label} <#if ((posted_by_userid != "") && (posted_by_userid != "Anonymous"))><@component id="common.widget.user-name" user="conv:${message.author.login}" /></#if></span>
                  </div>
                </div>
                <div class="custom-article-teaser">
                  <#if message.teaser?length gt 0>
                    ${message.teaser} <span class="custom-read-more"><a href="${message.@view_href}">${text.format("")}</a></span>
                  </#if>
                </div>
              </div>
            </#list>
          </#if>
        </div>
      </div>
    </li>
    
    • skylinegtr's avatar
      skylinegtr
      Mentor

      OlivierS- Thank you! It works but i'm getting this error. I've tried fixing the error but no luck. 

       

      FreeMarker template error

      The following has evaluated to null or missing:
      ==> message.@view_href  [in template "preview" at line 26, column 77]
      
      Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
      
      The failing instruction (FTL stack trace):
      ----------
      ==> ${message.@view_href}  [in template "preview" at line 26, column 75]
          #list messages.data.items as message  [in template "preview" at line 13, column 9]

       

    • irach15's avatar
      irach15
      Maven

      Hi OlivierS,

      I'm having a bit extended problem :-)

      I've done pretty much same solution as yours, but I also need to add a tkb sub-category title to each post/article links pulled from two categories. So it is linked to see all tkb articles in that tkb category.

      ----------------------------------

      #assign apiVersion = "2.0"/>
      <#assign x= rest(apiVersion,"/search?q=" + "SELECT id, subject, post_time, view_href, body FROM messages WHERE category.id IN('cat1','cat2') AND conversation.style = 'tkb' AND depth=0 ORDER BY post_time DESC LIMIT 5"?url) />

      <!-----    here is I think should be another call to get a category title??? --->

      <#if x.data.items ?? >
      <#list x.data.items as message >

      <div>

      <h2 class="message-subject">
      <a href="${message.view_href}">${message.subject}</a>

      </h2>

      <p><a href="${category.view_href}">${category.title}</a></p>

      <!----  here I'm not sure how to make another call  and show a category title and link, something like:  /t5/Management-Articles/tkb-p/some-TKB ---->

      </div>

       

      ---------------------

      Thank you