---
title: "Get and set custom field values"
slug: "get-and-set-custom-field-values"
abstract: "How to get and set the value of a SitecoreAI Page Builder custom field."
---
# Get and set custom field values

*Required extension points: SitecoreAI Page Builder custom field*

*Required SDK packages: client*

*Required API access: none*

> [!note]
> **Before you begin**
> -   Complete the [SDK quick start](https://doc.sitecore.com/mp/en/developers/sdk/0/sitecore-marketplace-sdk/quick-start--manual-.html "Quick start (manual)").

If your Marketplace app uses the [*SitecoreAI Page Builder custom field*](https://doc.sitecore.com/mp/en/developers/marketplace/page-builder-custom-fields.html) extension point, and if the custom field is [enabled](https://doc.sitecore.com/mp/en/developers/marketplace/enable-a-custom-field-in-the-sitecoreai-page-builder.html), you use the Marketplace SDK to read and write values into the custom field.

To get and set custom field values:

1. In the `src` folder, create a component, for example, `components/CustomField.tsx`, then save your changes.

    ```typescript
    // src/components/CustomField.tsx

    import { useState, useEffect } from "react";
    import { ClientSDK } from "@sitecore-marketplace-sdk/client";

    export default function CustomField({ client }: { client: ClientSDK | null }) {
      const [input, setInput] = useState<string>("");

      // Get the latest value on mount and whenever the client changes
      useEffect(() => {
        if (!client) return;
        const fetchValue = async () => {
          const latest = await client.getValue();

          // If latest is a JSON string, pretty print it
          if (typeof latest === "string") {
            try {
              const parsed = JSON.parse(latest);
              if (typeof parsed === "object" && parsed !== null) {
                setInput(JSON.stringify(parsed, null, 2));
                return;
              }
            } catch {
              // Not a JSON string, continue with the original string
            }
            setInput(latest);
          } else if (typeof latest === "object" && latest !== null) {
            setInput(JSON.stringify(latest, null, 2));
          } else {
            setInput(latest ? String(latest) : "");
          }
        };
        fetchValue();
      }, [client]);

      // Set the input value when the client changes
      const handleSave = async () => {
        try {
          await client?.setValue(input, true);
          client?.closeApp();
        } catch {
          // Optionally handle error
        }
      };

      return (
        <div>
          <textarea
            value={input}
            onChange={(e) => setInput(e.target.value)}
            placeholder="Enter value here"
            rows={4}
          />
          <button onClick={handleSave}>Save</button>
          <button onClick={() => client?.closeApp()}>Close</button>
        </div>
      );
    }
    ```

    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 `input` state: `const latest = await client.getValue(); setInput(latest);`
    - When the user clicks **Save**, stores the `textarea` value 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.
2. In the SDK initialization code where you run the `useMarketplaceClient` hook, import the component you created in the previous step:

    :::tab{title="react"}
    ## React

    ```typescript
    // src/App.tsx
    import CustomField from "./components/CustomField";
    ```

    :::
    :::tab{title="nextjs-pages"}
    ## Next.js (Pages Router)

    ```typescript
    // src/pages/index.tsx
    import CustomField from "@/components/CustomField";
    ```

    :::

3. In the same file, include the component in the return statement, then save your changes. Make sure to pass `client` to the component:

    ```typescript
    return (
      <>
        {/* ... */}
        <CustomField client={client} />
      </>
    );
    ```

4. In the SitecoreAI Page Builder, locate the custom field, then click **Open app**.
5. 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.