AEM SPA | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Raja_Reddy

Hi @rahulva4 

To access tags authored through the AEM dialog box in a React component in AEM, you can use the AEM Content Services API to fetch the tags from the AEM server. Here's an example of how you can do this:

First, you need to create a tag namespace in AEM. This can be done by navigating to the AEM Tags console (/libs/cq/tagging/gui/content/tags.html) and creating a new namespace.

Once you have created a tag namespace, you can author tags using the AEM dialog box. These tags will be stored in the AEM repository and can be accessed using the AEM Content Services API.

In your AEM component, you can use the ModelManager to fetch the tags from the AEM server. Here's an example:

import React, { useState, useEffect } from "react"; import { ModelManager } from "@adobe/aem-spa-page-model-manager"; function MyComponent(props) { const [tags, setTags] = useState([]); useEffect(() => { // Fetch the tags from the AEM server ModelManager.getData("/content/my-page/jcr:content/root/responsivegrid/my-component") .then((data) => { // Extract the tags from the data const tags = data["cq:tags"]; // Set the tags in the component state setTags(tags); }) .catch((error) => { console.error(error); }); }, []); return ( <div> {tags.map((tag) => ( <span key={tag}>{tag}</span> ))} </div> ); } export default MyComponent;

In this example, we use the ModelManager.getData method to fetch the data for the component from the AEM server. We then extract the tags from the data and set them in the component state using the useState hook. Finally, we render the tags as a list of span elements.

Thanks.

4 replies

Level 4
January 15, 2024

Hello @rahulva4 

 

When you are using React with AEM, data from AEM is passed to the React components as properties (props). Specifically for tags, once you have authored them using the AEM dialog box, these tags will be stored as properties of the resource. In your React component, you would have access to these tags through props by their property name. No specific API call will be needed to access these properties provided the mapping has been correctly set up.

Eg:

const MyReactComponent = (props) => {
const tags = props.tags;
// Do something with tags
}

 

Thanks,

Venkat

Mahedi_Sabuj
Community Advisor
Community Advisor
January 15, 2024

To expose dialog properties to the React component, need to implement a Sling Model. 

Refer to the Adobe documentation for guidance on creating a custom SPA component
https://experienceleague.adobe.com/docs/experience-manager-learn/getting-started-with-aem-headless/spa-editor/react/custom-component.html?lang=en

 

Mahedi Sabuj
Raja_Reddy
Community Advisor
Raja_ReddyCommunity AdvisorAccepted solution
Community Advisor
January 15, 2024

Hi @rahulva4 

To access tags authored through the AEM dialog box in a React component in AEM, you can use the AEM Content Services API to fetch the tags from the AEM server. Here's an example of how you can do this:

First, you need to create a tag namespace in AEM. This can be done by navigating to the AEM Tags console (/libs/cq/tagging/gui/content/tags.html) and creating a new namespace.

Once you have created a tag namespace, you can author tags using the AEM dialog box. These tags will be stored in the AEM repository and can be accessed using the AEM Content Services API.

In your AEM component, you can use the ModelManager to fetch the tags from the AEM server. Here's an example:

import React, { useState, useEffect } from "react"; import { ModelManager } from "@adobe/aem-spa-page-model-manager"; function MyComponent(props) { const [tags, setTags] = useState([]); useEffect(() => { // Fetch the tags from the AEM server ModelManager.getData("/content/my-page/jcr:content/root/responsivegrid/my-component") .then((data) => { // Extract the tags from the data const tags = data["cq:tags"]; // Set the tags in the component state setTags(tags); }) .catch((error) => { console.error(error); }); }, []); return ( <div> {tags.map((tag) => ( <span key={tag}>{tag}</span> ))} </div> ); } export default MyComponent;

In this example, we use the ModelManager.getData method to fetch the data for the component from the AEM server. We then extract the tags from the data and set them in the component state using the useState hook. Finally, we render the tags as a list of span elements.

Thanks.

kautuk_sahni
Community Manager
Community Manager
January 15, 2024

@rahulva4 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni