+ Start a Discussion
MCordMCord 

How can you retrieve a list of Community Navigational Topics in APEX? 

The Topic Catalog Component proves that it is possible to create a component that lists out just the nav topics. How do we replicate this?
Best Answer chosen by MCord
Amil_AbdallahAmil_Abdallah
This is currently possible using the Connect API.  Please see the following URL for the ManagedTopics Class using the Connect API 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_ConnectAPI_ManagedTopics_static_methods.htm#apex_ConnectAPI_ManagedTopics_static_methods

Below is a reference to return a list of Navigation Topics defined in your community.  Create a TopicController class referencing the methods listed in the url above.  
public class TopicController {

    public static ConnectAPI.ManagedTopicCollection getNavigationTopics(){
        return ConnectAPI.ManagedTopics.getManagedTopics('YourCommunityId', 
        ConnectApi.ManagedTopicType.Navigational);
    } 
}
Using your new TopicController class, here is an example on how you would go about displaying the results:
You can run this in Execute Anonymous:

for(ConnectApi.ManagedTopic mtopic : TopicController.getNavigationTopics().managedTopics){
    System.debug(mtopic.topic.name);
}

 

All Answers

MagulanDuraipandianMagulanDuraipandian
Hi,
I think it is not supported. Vote for this idea - https://success.salesforce.com/ideaView?id=08730000000cFIAAA2
Amil_AbdallahAmil_Abdallah
This is currently possible using the Connect API.  Please see the following URL for the ManagedTopics Class using the Connect API 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_ConnectAPI_ManagedTopics_static_methods.htm#apex_ConnectAPI_ManagedTopics_static_methods

Below is a reference to return a list of Navigation Topics defined in your community.  Create a TopicController class referencing the methods listed in the url above.  
public class TopicController {

    public static ConnectAPI.ManagedTopicCollection getNavigationTopics(){
        return ConnectAPI.ManagedTopics.getManagedTopics('YourCommunityId', 
        ConnectApi.ManagedTopicType.Navigational);
    } 
}
Using your new TopicController class, here is an example on how you would go about displaying the results:
You can run this in Execute Anonymous:

for(ConnectApi.ManagedTopic mtopic : TopicController.getNavigationTopics().managedTopics){
    System.debug(mtopic.topic.name);
}

 
This was selected as the best answer
Andrew FandreAndrew Fandre
Just to clarify on anyone wanting the code to get your community ID and populate it into a list of Topics for consumption in a lightning component.

In your APEX Controller for your component add this function.

@AuraEnabled
public static List<ConnectApi.Topic> getNavigationTopics(){
        string commId = [Select Id from Network where Name = 'YOUR COMMUNITY NAME'].Id;
        ConnectApi.ManagedTopicCollection mtCollection = ConnectAPI.ManagedTopics.getManagedTopics(commId, ConnectApi.ManagedTopicType.Navigational);
        List<ConnectApi.Topic> topicList = new List<ConnectApi.Topic>();
        for(ConnectApi.ManagedTopic mtopic : mtCollection.managedTopics)
        {
            topicList.add(mtopic.topic);
        }
       return topicList;
    }
 
Then in your Component controller add this

loadTopics: function (component, event, helper) {
var action = component.get("c.getNavigationTopics");
        action.setCallback(this, function(response){
            var state = response.getState();
            
            if(state==="SUCCESS"){
                var responses = response.getReturnValue();
                component.set("v.managedTopics", responses);
            } else {
                console.log("error occured retrieving topics");
            }
            
        });
        
        $A.enqueueAction(action);

}

Then in your component add this.

<aura:attribute name="managedTopics" type="ConnectApi.ManagedTopic[]" />
<aura:iteration items="{!v.managedTopics}" var="topic">

{!topic.name}

</aura:iteration>

Srini GrandhiSrini Grandhi
Andrew, most of the code worked for me. I changed the component code to call loadTopics() on init. This is my component code:
==================================================================================================
<aura:component implements="forceCommunity:availableForAllPageTypes" access="global" controller="CommunityHelper">
    <aura:attribute name="managedTopics" type="ConnectApi.ManagedTopic[]" />
    <aura:handler name="init" value="{!this}" action="{!c.loadTopics}"/> 
    
    <aura:iteration items="{!v.managedTopics}" var="topic">
    
    {!topic.name}
    
    </aura:iteration>
</aura:component> 
Robin SandlinRobin Sandlin
Excellent work Andrew and Srini  !!!