liferaywebcontent

Get user-specific web-content


I would like to ask if there is any Java API call in liferay which returns the web contents, which were uploaded by a specific user.

For example, I have one user who has upload some content and I want to show in a portlet this content, how can I do this via java?


Solution

  • If you are specifically talking about web-content which is displayed inside Web-content Display portlet, then you can use the JournalArticleService and JournalArticleLocalService API to fetch the content depending upon the User.
    Prior to Liferay 6.0 Web-content was known as JournalArticle and hence the API name has not changed.

    So for example:

    You can use DynamicQuery API, as follows:

    long userId = 10987L; // ofcourse you need to find this
    
    DynamicQuery dynamicQuery = JournalArticleLocalServiceUtil.dynamicQuery();
    
    dynamicQuery.add(RestrictionsFactoryUtil.eq("userId", userId));
    
    int startOfList = 0;
    int endOfList = 1000;
    // if you want all the JournalArticle retrieved then use:
    // int endOfList = QueryUtil.ALL_POS;
    
    // this will retrieve the list of webcontents
    List<JournalArticle> articles = (List<JournalArticle>) JournalArticleLocalServiceUtil.dynamicQuery(dynamicQuery, startOfList, endOfList);
    

    The above code will retrieve all the JournalArticles so you would get all the versions of a single web-content since all these versions are stored in the same JournalArticle table. So for this you can add conditions to the dynamicQuery for the fields like version, id, resourcePrimKey, articleId, groupId, companyId etc.

    Or if you have more complex needs than you can create a custom-sql-finder in liferay to fetch the desired data from any combination of Liferay DB tables.


    If you are talking about contents as in Blogs, Wikis, Files, Webcontents etc then either use their respective *LocalServiceUtil or you can use AssetEntryLocalServiceUtil to fetch the assets for a particular User.

    So with AssetEntryLocalServiceUtil also you can use the DynamicQuery API as shown above. The code may not be same but will be along the same lines.


    You can know more about DynamicQuery API from this blog.