Thursday, April 3, 2014

JSON object parameter passing

I came across a scenario where I need to use Liferay's JSON API for display certain no of documents under each folder depending upon configuration/preferences selected/set.

I need to display certain # of records order by modified date descending order

I didn't find a one workable example when I googled for certain time, here comes the savior - liferay documentation helped me in this regard to say frankly

Here is the code snippet I used

        var groupId = '<%= themeDisplay.getLayout().getGroupId() %>';
        var folderNames = ["Folder 1", "Folder 2", "Folder 3"]; //The names of the folders you wish to pull from and display;
        var auth = "<%= AuthTokenUtil.getToken(request) %>"; //url to the file that prints out the p_auth token

        function ajax(url, func, type) {
            result = '';
                type: 'GET',
                url: url,
                func: func,
                dataType: type,
                async: false,
                success: function (data) {
                    result = data;
            return result;

        jQuery(document).ready(function () {
            var html = '';
            var quickHitshtml = '';
            var whitePapershtml = '';
            var noOfDocsToDisplay = '<%= noOfMarketCommentaryDocs %>'; // reading value from preferences
            jQuery.each(folders, function (i, item) {
                if (jQuery.inArray(, folderNames) > -1) {
                    var files = ajax("/api/jsonws/dlapp/get-file-entries/repository-id/" + groupId + "/folder-id/" + item.folderId + "/start/0/end/" + noOfDocsToDisplay + "/+obc:com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelModifiedDateComparator?p_auth=" + auth + "&callback=?", 'files', 'jsonp');

please add relavent imports & porltet/theme should be loaded with jQuery

1 comment:

  1. Hi

    Its a great snippet... can you comment how we can call a json ws api and populate the object parameter with our own data... for reference you can check below question: