Forum Discussion
Hi
As far as some of your specific API questions, take a look at this:
It has some examples that include the body and other fields like tags, labels and attachments.
Then for using this inside a component with freemarker take a look at this:
It has an example of how to do this inside a component with freemarker. You would not use curl within a component, and you would not need to authenticate.
However, as cblown says this wouldn't work well for what you are trying to do, but just wanted to share because these are better examples than what you are finding in the "Recipes" section.
Also, if you look at the menu on the left side there are a lot of other good examples.
Also, there are some tutorials to help you get started creating components.
Tutorial: Write your first custom component (khoros.com)
Tutorial: Update a component from Community API v1 to v2 (khoros.com)
Also, this has some videos tutorials. I watched it a while ago so don't remember how good it was but might be helpful.
Could you do me the favor of showing me a simple example in place in a Freemarker custom component?
Thanks
Sure. There are a few different ways to make an API call inside a Khoros component with freemarker. If you have a body like when creating a message, I like to use restBuilder. The restBuilder documentation has an example of this. I'll share it below.
<#assign messagePostCall = restBuilder() .method("POST") .path("/messages") .body({ "type": "message", "board": { "type": "board", "id": myBoard }, "subject": "Test Message", "body": "This is a test" }) .admin(true) /> <#assign resp = messagePostCall.call() />
Something to keep in mind, freemarker runs server side before the page loads. So, if you need any user input you have to use javascript. You can put your freemarker in a custom endpoint About endpoints (khoros.com) which is maybe getting a little advanced if you are just getting started with Khoros.
Khoros has other freemarker objects for making API calls you can use as well.
restadmin and liqladmin are just like rest and liql, but they make the calls with admin permissions. Keep in mind you don't need to authenticate from inside a khoros component because the community knows you are logged in and who you are. However, that means it will only let you do things you have permission for, so if you are making a component that will be accessible by regular members, but you need to do something that requires admin permissions you would use the restadmin version. Just be careful not to accidently reveal private info or anything to regular community members.
Also, I'm not sure if you are familiar with LiQL yet. It lets you get information with a query formatted like this.
SELECT author.id FROM messages WHERE board.id = 'xyz'
liql and liqladmin let you do that very easily, so if you use LiQL a lot they are very handy. For example, from the documentation:
Compare liql call to the rest call:
rest("2.0","/search?q=" + "SELECT id, subject, body FROM messages ORDER BY post_time DESC LIMIT 5"?url)with liql call:
liql("SELECT id, subject, body FROM messages ORDER BY post_time DESC LIMIT 5")
Something else to keep in mind, you can use any of the typical web development stuff in a component too. So, if you wanted to or if the situation demanded it, you could also use JavaScript to make API calls. You can use a regular script tag and add JavaScript like you would in HTML or use liaAddScript (khoros.com) which is what I usually do. It is freemarker provided by Khoros for that purpose. However, then you wouldn't have access to restadmin or anything like that and you need to keep in mind permissions and who is going to be using the component.
Hopefully that's not too much information. I would start with something simple for practice and then go from there. The documentation can be hard to navigate but I lean heavily on it. Once you learn your way around there it is very useful.
Also, if you want some real examples.
This component is for a custom page that shows a user's unsolved topics. There are some liql calls on line 10 and 17.
Keneficka/topics-without-solutions: Adds an unsolved posts tab to the profile page. (github.com)
And this is a tabbed forum page with various filters. There is an example of restBuilder starting on line 112. I store the results in a variable "resp" on line 167. Then use freemarker "list" to loop through the results starting on line 197.
tabbed-forum-page/custom-tabbed-message-list.ftl at main · Keneficka/tabbed-forum-page (github.com)
I don't think I've ever done anything that creates a message, but I am working on something very similar now that would create an abuse report on an archived message to request it be unarchived. Create message abuse report (khoros.com) It will use an endpoint. I can share that when I'm finished.
Related Content
- 2 years ago
- 7 months ago
- 2 years ago