Organizations API Basics

You can Create and Manage Organizations from the Application menu, but you can also use Liferay’s REST APIs. Call these services to manage organizations.

Adding an Organization

Start a new Liferay DXP instance by running

docker run -it -m 8g -p 8080:8080 liferay/dxp:2024.q1.1

Sign in to Liferay at http://localhost:8080 using the email address test@liferay.com and the password test. When prompted, change the password to learn.

Then, follow these steps:

  1. Download and unzip Organizations API Basics.

    curl https://resources.learn.liferay.com/dxp/latest/en/users-and-permissions/developer-guide/liferay-w2h3.zip -O
    
    unzip liferay-w2h3.zip
    
  2. Use the cURL script to add a new Organization to your instance. On the command line, navigate to the curl folder. Execute the Organization_POST_ToInstance.sh script.

    ./Organization_POST_ToInstance.sh
    

    The JSON response shows a new Organization has been added:

    {
      "comment": "",
      "customFields": [],
      "dateCreated": "2022-05-19T17:38:19Z",
      "dateModified": "2022-05-19T17:38:19Z",
      "externalReferenceCode": "",
      "id": "40922",
      "keywords": [],
      "location": {},
      "name": "Able",
      "numberOfAccounts": 0,
      "numberOfOrganizations": 0,
      "numberOfUsers": 0,
      "organizationContactInformation": {
        "emailAddresses": [],
        "postalAddresses": [],
        "telephones": [],
        "webUrls": []
      }
    }
    
  3. Navigate to Global MenuControl PanelUser and Organizations. Click the Organizations tab. See that a new Organization has been added.

    See that a new organization has been added.

  4. The REST service can also be called using the Java client. Navigate out of the curl folder and into the java folder. Compile the source files with the following command:

    javac -classpath .:* *.java
    
  5. Run the Organization_POST_ToInstance.java class with the following command.

    java -classpath .:* Organization_POST_ToInstance
    

Examine the cURL Command

The Organization_POST_ToInstance.sh script calls the REST service with a cURL command.

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/organizations" \
	--data-raw '
		{
			"name": "Able"
		}' \
	--header "Content-Type: application/json" \
	--request "POST" \
	--user "test@liferay.com:learn"

Here are the command’s arguments:

ArgumentsDescription
-H "Content-Type: application/json"Indicates that the request body format is JSON.
-X POSTThe HTTP method to invoke at the specified endpoint
"http://localhost:8080/o/headless-admin-user/v1.0/organizations"The REST service endpoint
-d "{\"name\": \"Able\"}"The data you are requesting to post
-u "test@liferay.com:learn"Basic authentication credentials
note

Basic authentication is used here for demonstration purposes. For production, you should authorize users via OAuth2. See Using OAuth2 to Authorize Users for a sample React application that uses OAuth2.

The other cURL commands use similar JSON arguments.

Examine the Java Class

The Organization_POST_ToInstance.java class adds an organization by calling the Organization-related service.

public static void main(String[] args) throws Exception {
	OrganizationResource.Builder builder = OrganizationResource.builder();

	OrganizationResource organizationResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Organization organization = organizationResource.postOrganization(
		new Organization() {
			{
				name = "Dog";
			}
		});

	System.out.println(organization);
}

This class invokes the REST service using only three lines of code:

Line (abbreviated)Description
OrganizationResource.Builder builder = ...Gets a Builder for generating an OrganizationResource service instance.
OrganizationResource organizationResource = builder.authentication(...).build();Specifies basic authentication and generates a OrganizationResource service instance.
Organization organization = organizationResource.postOrganization(...);Calls the organizationResource.postOrganization method and passes the data to post.

Note that the project includes the com.liferay.headless.admin.user.client.jar file as a dependency. You can find client JAR dependency information for all REST applications in the API explorer in your installation at /o/api.

note

The main method’s comment demonstrates running the class.

The other example Java classes are similar to this one, but call different OrganizationResource methods.

important

See OrganizationResource for service details.

Below are examples of calling other Organization REST services using cURL and Java.

Get Organizations from Instance

You can list Organizations by executing the following cURL or Java command.

Organizations_GET_FromInstance.sh

Command:

./Organizations_GET_FromInstance.sh

Code:

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/organizations" \
	--user "test@liferay.com:learn"

Organizations_GET_FromInstance.java

Command:

java -classpath .:* Organizations_GET_FromInstance

Code:

public static void main(String[] args) throws Exception {
	OrganizationResource.Builder builder = OrganizationResource.builder();

	OrganizationResource organizationResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Page<Organization> page = organizationResource.getOrganizationsPage(
		null, null, null, Pagination.of(1, 2), null);

	System.out.println(page);
}

The Instance’s Organization objects appear in JSON.

Get an Organization

Get a specific Organization with the following cURL or Java command.

tip

Use Organizations_GET_FromInstance.[java|sh] to get instance Organization IDs.

Organization_GET_ById.sh

Command:

./Organization_GET_ById.sh 1234

Code:

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/organizations/${1}" \
	--user "test@liferay.com:learn"

Organization_GET_ById.java

Command:

java -classpath .:* -DorganizationId=1234 Organization_GET_ById

Code:

public static void main(String[] args) throws Exception {
	OrganizationResource.Builder builder = OrganizationResource.builder();

	OrganizationResource organizationResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	System.out.println(
		organizationResource.getOrganization(
			String.valueOf(System.getProperty("organizationId"))));
}

The Organization fields appear in JSON.

Patch an Organization

Do a partial edit of an existing Organization with the following cURL and Java commands. Replace 1234 with your Organization’s ID.

Organization_PATCH_ById.sh

Command:

./Organization_PATCH_ById.sh 1234

Code:

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/organizations/${1}" \
	--data-raw '
		{
			"name": "Baker"
		}' \
	--header "Content-Type: application/json" \
	--request "PATCH" \
	--user "test@liferay.com:learn"

Organization_PATCH_ById.java

Command:

java -classpath .:* -DorganizationId=1234 Organization_PATCH_ById

Code:

public static void main(String[] args) throws Exception {
	OrganizationResource.Builder builder = OrganizationResource.builder();

	OrganizationResource organizationResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Organization organization = organizationResource.patchOrganization(
		String.valueOf(System.getProperty("organizationId")),
		new Organization() {
			{
				name = "Easy";
			}
		});

	System.out.println(organization);
}

Put an Organization

Completely overwrite an existing Organization with the following cURL and Java commands. Replace 1234 with your Organization’s ID.

Organization_PUT_ById.sh

Command:

./Organization_PUT_ById.sh 1234

Code:

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/organizations/${1}" \
	--data-raw '
		{
			"name": "Charlie"
		}' \
	--header "Content-Type: application/json" \
	--request "PUT" \
	--user "test@liferay.com:learn"

Organization_PUT_ById.java

Command:

java -classpath .:* -DorganizationId=1234 Organization_PUT_ById

Code:

public static void main(String[] args) throws Exception {
	OrganizationResource.Builder builder = OrganizationResource.builder();

	OrganizationResource organizationResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Organization organization = organizationResource.putOrganization(
		String.valueOf(System.getProperty("organizationId")),
		new Organization() {
			{
				name = "Fox";
			}
		});

	System.out.println(organization);
}

Delete an Organization

Delete an existing Organization with the following cURL and Java commands. Replace 1234 with your Organization’s ID.

Organization_DELETE_ById.sh

Command:

./Organization_DELETE_ById.sh 1234

Code:

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/organizations/${1}" \
	--request "DELETE" \
	--user "test@liferay.com:learn"

Organization_DELETE_ById.java

Command

java -classpath .:* -DorganizationId=1234 Organization_DELETE_ById

Code:

public static void main(String[] args) throws Exception {
	OrganizationResource.Builder builder = OrganizationResource.builder();

	OrganizationResource organizationResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	organizationResource.deleteOrganization(
		String.valueOf(System.getProperty("organizationId")));
}

The API Explorer shows all of the Organization services and schemas and has an interface to try out each service.

Note

Get organization postal addresses with PostalAddresses_GET_FromOrganization

Capabilities

Product

Contact Us

Connect

Powered by Liferay
© 2024 Liferay Inc. All Rights Reserved • Privacy Policy