Liferay

Attach categories and fill data in custom fields of webcontent with the Liferay Headless API

Jigna Patel
Jigna PatelNov 8, 2022

Problem

Attach categories and custom values to the webcontent with the help of the rest api.

Solution

We can use Lifeay’s headless API, which creates the webconetnt programmatically. And attach the categories and fill in the values of custom fields.

Benefits

  • We can use this api to add and update the webcontent to attach the categories and access custom fields from the Rest API.

Prerequisites

  • Java
  • Liferay portal 7.2 +
  • Basic knowledge of Liferay

Environment Requirements

  • JDK 11
  • Liferay Portal
  • Postman

Follow the following steps to post a webcontent on the Liferay Site

We have two ways to post a webcontent in liferay which are as follows.

  1. From a postman.
  2. From the Liferay Rest API editor

1) From the postman:

1. Add a new request and make the request type “post“.

2. Enter the following URL in the “Enter Requested URL”.

1http://localhost:8080/o/headless-delivery/v1.0/sites/${siteId}/structured-contents
  • You have to enter the site Id in the url in which site you have to create the webcontent.

3. Now, we have to assign the authorization.

  • Select the type as Basic Auth.
  • Enter the username and password of the admin user.
Blog Image

4. Select Body from the Navigation on the postman.

5. Select “raw” from the first username and select “JSON” from the second drop-down menu.

Blog Image

6. Enter the following Json Object.

1{
2 "actions": {
3 },
4 "availableLanguages": [
5   "en-US",
6   "ar-SA"
7 ],
8 "contentFields": [
9   {
10           "contentFieldValue": {
11               "document": {
12               "id": 113029
13               }
14           },
15           "contentFieldValue_i18n": {
16               "ar-SA": {
17                   "document": {
18                       "id": 113019
19                   }
20               },
21               "en-US": {
22                   "document": {
23                       "id": 113029
24                   }
25               }
26           },
27           "dataType": "document",
28           "label": "File Upload",
29           "label_i18n": {
30               "en-US": "File Upload"
31           },
32           "name": "file_upload"
33       }
34 ],
35 "contentStructureId": 50803,
36 "customFields": [
37   {
38     "customValue": {
39       "data": 2021
40     },
41     "dataType": "Integer",
42     "name": "ReferencePeriodYear"
43   },
44   {
45     "customValue": {
46       "data": "Quarterly"
47     },
48     "dataType": "Text",
49     "name": "Periodicity"
50   },
51   {
52     "customValue": {
53       "data": "2022-01-17T15:27:58Z"
54     },
55     "dataType": "",
56     "name": "IssueDate"
57   },
58   {
59     "customValue": {
60       "data": "Producer Price Index, Third quarter 2021"
61     },
62     "dataType": "Text",
63     "name": "PublicationName"
64   },
65   {
66     "customValue": {
67       "data": 11581
68     },
69     "dataType": "Integer",
70     "name": "ReleaseID"
71   },
72   {
73     "customValue": {
74       "data": "Both"
75     },
76     "dataType": "Text",
77     "name": "Language"
78   },
79   {
80     "customValue": {
81       "data": "ربع سنوي"
82     },
83     "dataType": "Text",
84     "name": "PeriodicityAr"
85   },
86   {
87     "customValue": {
88       "data": "الرقم القياسي لأسعار المنتجين الصناعيين الربع الثالث 2021"
89     },
90     "dataType": "Text",
91     "name": "PublicationNameAr"
92   },
93   {
94     "customValue": {
95       "data": "الربع الثالث"
96     },
97     "dataType": "Text",
98     "name": "IntervalAr"
99   },
100   {
101     "customValue": {
102       "data": "2022-01-17T00:00:00Z"
103     },
104     "dataType": "",
105     "name": "UIIssueDate"
106   }
107 ],
108 "datePublished": "2021-08-30T02:14:00Z",
109 "siteId": 39628,
110 "taxonomyCategoryIds": [3066,3073,3067,3082],
111 "title": "Webcontent created for the testing",
112 "title_i18n": {
113   "ar-SA": "تم إنشاء نص ملف جديد آخر",
114   "en-US": "Webcontent created for the testing"
115 }
116}

Brief description of the JSON keys are following.

1. availableLanguages: -

  • Enter the languages that the web contents will support the localization.

2. ContentFields :-

  • Content Fields will be responsible for giving the data to the structured input that we have used for web content creation.
  • In our case, we have a repeatable field in the structure of Web content

3. ContentFieldValue : -

  • Content Field Value will fill in the data in the structure of Web content.
  • In our case, we have the repeatable file upload field. That's why we are providing the document details under the content field value.
  • In the “contentFieldValue” key only, it will contain the data of the default-language set on the liferay.
  • If we want to localize it. This means if we want to add a document to the language ID, then we have to use “contentFieldValue_i18n”.

4. contentFieldValue_i18n : -

  • This Content Field Value will be responsible for the localization of the Webcontent.
  • Webcontent Data Will Be Different According to Their Locale (Example :- “en-Us” and “ar-SA”).
  • As per the localization, we have to provide the document ID for the different language support.

I. ar-SA :-

  • We have to provide the document details in this language to attach the document to the Arabic version of the web content.

II. en-US :-

  • We have to provide the document details in this language to attach the document to the English version of the web content.

5. document : -

  • Document will be responsible for attaching the document file to the webcontent.

6. Id : -

  • In this ID key, we have to provide the document ID of the document that we have to attach.

7. dataType : -

  • This will be the data type of the structured content field.

8. label : -

  • Enter the languages that the web contents will support the localization.

9. label_i18n : -

  • label_i18n will be responsible for the localization as per “contentFieldValue_i18n”.

10. name : -

  • Enter the languages that the web contents will support the localization.

11. contentStructureId :-

  • This will be the structure ID of the web content that will be created.

12. customFields :-

  • This field is responsible for the custom fields that are created for the web content article.

13. customValue :-

  • This json will be responsible for the value assignment of the custom fields.

14. Data :-

  • This data will be the value of a particular custom field defined in the json of customFields “name” attribute.

15. Name :-

  • This name will represent the name of the custom fields that are created for the web content.

16. dataType :-

  • This dataType represents the data type of the custom field of the webcontent.

17. datePublished :-

  • This will set the “Display Date” over the Web content fields.

18. siteId :-

  • Site ID: Specify the site id in which we are going to create web content.

19. taxonomyCategoryIds :-

  • We have to assign the category ID array to attach the different categories to the Web content.
  • For example:-
1"taxonomyCategoryIds": [3066,3077,3082],

20. title :-

  • Title of the Webcontent We have to enter the title of the default language IDtitles of the Liferay .

21. title_i18n :-

  • This title_i18n will be responsible for the localization.
  • We have to enter the titles of different languages that we have allowed in the “availableLanguages”.
  • Like the following.

I. ar-SA :-

  • We want titles in Arabic, so we will put the Arabic conversion or the Arabic version title of webcontent.

7. After adding the json object to the post Body.

8. Post the request and if you get 200 response codes, then webcontent has been added to the Liferay site.

Blog Image

9. Make sure you get the success code 200 and you will get the response.

10. If you get a success code 200, but if you do not get the response, then there is some issue with the authentication.

Note:- Make sure you enter the URL and do not have a blank space after the URL completion.


2) From the Liferay Rest API Editor :-

1. Login as an admin on the Liferay Portal.

2. Access the following URL to access the Liferay Swagger editor.

1http://localhost:8080/o/api

3. Access the accordion called “StructuredContent”.

Blog Image

4. Enter the site ID that you want to create the webcontent.

Blog Image

5. Enter the JSON object in the request body.

1{
2 "actions": {
3 },
4 "availableLanguages": [
5   "en-US",
6   "ar-SA"
7 ],
8 "contentFields": [
9   {
10           "contentFieldValue": {
11               "document": {
12               "id": 113029
13               }
14           },
15           "contentFieldValue_i18n": {
16               "ar-SA": {
17                   "document": {
18                       "id": 113019
19                   }
20               },
21               "en-US": {
22                   "document": {
23                       "id": 113029
24                   }
25               }
26           },
27           "dataType": "document",
28           "label": "File Upload",
29           "label_i18n": {
30               "en-US": "File Upload"
31           },
32           "name": "file_upload"
33       }
34 ],
35 "contentStructureId": 50803,
36 "customFields": [
37   {
38     "customValue": {
39       "data": 2021
40     },
41     "dataType": "Integer",
42     "name": "ReferencePeriodYear"
43   },
44   {
45     "customValue": {
46       "data": "Quarterly"
47     },
48     "dataType": "Text",
49     "name": "Periodicity"
50   },
51   {
52     "customValue": {
53       "data": "2022-01-17T15:27:58Z"
54     },
55     "dataType": "",
56     "name": "IssueDate"
57   },
58   {
59     "customValue": {
60       "data": "Producer Price Index Third quarter 2021"
61     },
62     "dataType": "Text",
63     "name": "PublicationName"
64   },
65   {
66     "customValue": {
67       "data": 11581
68     },
69     "dataType": "Integer",
70     "name": "ReleaseID"
71   },
72   {
73     "customValue": {
74       "data": "Both"
75     },
76     "dataType": "Text",
77     "name": "Language"
78   },
79   {
80     "customValue": {
81       "data": "ربع سنوي"
82     },
83     "dataType": "Text",
84     "name": "PeriodicityAr"
85   },
86   {
87     "customValue": {
88       "data": "الرقم القياسي لأسعار المنتجين الصناعيين الربع الثالث 2021"
89     },
90     "dataType": "Text",
91     "name": "PublicationNameAr"
92   },
93   {
94     "customValue": {
95       "data": "الربع الثالث"
96     },
97     "dataType": "Text",
98     "name": "IntervalAr"
99   },
100   {
101     "customValue": {
102       "data": "2022-01-17T00:00:00Z"
103     },
104     "dataType": "",
105     "name": "UIIssueDate"
106   }
107 ],
108 "datePublished": "2021-08-30T02:14:00Z",
109 "siteId": 39628,
110 "taxonomyCategoryIds": [3066,3073,3067,3082],
111 "title": "Webcontent created for the testing",
112 "title_i18n": {
113   "ar-SA": "تم إنشاء نص ملف جديد آخر",
114   "en-US": "Webcontent created for the testing"
115 }
116}

6. For the explanation of JSON objects, their keys are written in the Postman way. Please go through with that.

7. Execute the Rest API.

8. Make sure you get the success code 200 and you will get the response.

Blog Image

9. Your webcontent has been created successfully.

© 2026 IGNEK. All rights reserved.

Ignek on LinkedInIgnek on InstagramIgnek on FacebookIgnek on YouTubeIgnek on X