đ Continuation: Saving Sitecore XM Cloud Form Data to SQL Database
This blog is a continuation of my previous post: Customizing XM Cloud Form Component. In this article, I will walk you through how to save Sitecore XM Cloud form submissions to a SQL Server database using the XM Cloud Starter Kit.
đ ️ Sitecore CMS Changes
1. Create an XM Cloud Form
I assume you already know how to create forms in XM Cloud. If not, please check the official Sitecore documentation.
2. Duplicate and Customize the Form Component
Duplicate the existing form component from the XM Cloud Starter Kit and customize it as needed. I covered this process in detail in my previous blog post:
đ Customizing XM Cloud Form Component
đ§ą XM Cloud Starter Kit Code Changes
Step 1: Install MSSQL Package
In your Next.js starter kit, install the mssql package:
Step 2: Create a Webhook
Navigate to the following directory in your project:
Create a webhook, file named saveformdata.ts (or submit-form.ts). Paste the following code into that file:
đ Code Explanation
đ SQL Server Configuration
Update this section of the code with your local SQL Server details:
⚠️ Make sure
encryptandtrustServerCertificatevalues match your environment setup.
đ§Š Form Data Destructuring
This part of the code extracts data from the form submission:
Make sure the keys (like text_cf4be, email_1c499) match the field IDs from your custom form.
đž Inserting Data into SQL Server
Here’s where the form data is saved using a parameterized query:
This helps prevent SQL injection and safely stores the data.
đ Common Issue & Fix
After setting everything up, you might run into this error:
"SQL Server connection timeout / not reachable"
✅ Fix:
You need to start the SQL Server Browser service manually:
-
Go to Windows Services (
services.msc) -
Locate SQL Server Browser
-
Right-click → Start
Once started, retry submitting the form.
✅ Final Verification
Step 1: Submit the Form
Go to your form page and fill in the form details.
Step 2: Check SQL Server
Open SQL Server Management Studio (SSMS) and run:
You should see your form data saved successfully đ
đ Conclusion
That’s it! You’ve now connected your Sitecore XM Cloud form to a SQL Server database using the XM Cloud Starter Kit and a simple API webhook.
If you haven't customized your form yet, make sure to follow the steps in my previous blog post here:
đ Customizing XM Cloud Form Component
Comments
Post a Comment