Get and set custom field values
-
Complete the SDK quick start.
If your Marketplace app uses the SitecoreAI Page builder custom field extension point, and if the custom field is enabled, you use the Marketplace SDK to read and write values into the custom field.
To get and set custom field values:
-
In the
srcfolder, create a component, for example,components/CustomField.tsx, then save your changes.This script:
-
Stores user input in state:
const [input, setInput] = useState<string>(""); -
When the app loads, gets the current custom field value and stores it in the
inputstate:const latest = await client.getValue(); setInput(latest); -
When the user clicks Save, stores the
textareavalue in Sitecore and closes the app modal:await client?.setValue(input, true); client?.closeApp(); -
Returns a Save button that stores the changes and closes the app's modal.
-
-
In the SDK initialization code where you run the
useMarketplaceClienthook, import the component you created in the previous step: -
In the same file, include the component in the return statement, then save your changes. Make sure to pass
clientto the component: -
In the SitecoreAI Page builder, locate the custom field, then click Open app.
-
In the modal where your Marketplace app appears, in the
textarea, enter a value, such as{ "icon": "pencil" }, then click Save.The custom field displays the entered value.