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
https://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/json-web-services
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 = '';
jQuery.ajax({
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(item.name, 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
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
https://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/json-web-services
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 = '';
jQuery.ajax({
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(item.name, 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
Hi
ReplyDeleteIts 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:
https://www.liferay.com/community/forums/-/message_boards/message/47219298
and
https://www.liferay.com/community/forums/-/message_boards/message/48143910