Skip to content

Enable support for XM Cloud Forms #21

Open

Description

@robearlam

What would you add to the SDK?

The SDK needs to support the ability to leverage XM Cloud Forms Components.

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added
    enhancement
    on Oct 1, 2024
  2. jballe commented on Oct 11, 2025

    @jballe
    Contributor

    I don't know the details of the inner workings of forms... However...
    I got an initial version working by requesting the html from the edge

    [ViewComponent(Name = ViewComponentName)]
    public class FEaasFormViewComponent(IViewModelBinder binder, FormsClient formsClient) : ViewComponent
    {
        public const string ViewComponentName = "Form";
    
        public async Task<IViewComponentResult> InvokeAsync(CancellationToken cancellationToken)
        {
            var renderingContext = HttpContext?.GetSitecoreRenderingContext();
            var model = await binder.Bind<FEeaSFormModel>(ViewContext).ConfigureAwait(false);
    
            model.FormHtml = await GetFormHtml(model, cancellationToken);
    
            return View("Form", model);
        }
    
        private ValueTask<string> GetFormHtml(FEeaSFormModel model, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(model.FormId))
            {
                return ValueTask.FromResult(string.Empty);
            }
    
            return formsClient.GetFormHtmlAsync(model.FormId, cancellationToken);
        }
    }
    
    public class FormsClient(IHttpClientFactory httpClientFactory, IOptions<SitecoreSettings> sitecoreSettings)
    {
        public async ValueTask<string> GetFormHtmlAsync(string formId, CancellationToken cancellationToken)
        {
            var edgeContextId = sitecoreSettings.Value.EdgeContextId;
            var url = $"https://edge-platform.sitecorecloud.io/v1/forms/publisher/{formId}?sitecoreContextId={edgeContextId}";
            using var client = httpClientFactory.CreateClient();
            var result = await client.GetStringAsync(url, cancellationToken);
            return result;
        }
    }
    
    public class FEeaSFormModel : BaseComponentModel
    {
        [SitecoreComponentParameter]
        public string? FormId { get; set; }
    
        public string FormHtml { get; set; } = string.Empty;
    }

    (we have a cached version using IHybridCache)
    I am sure the edge url should be configured for you to support internal lower environments.

    My question is more... is this "just" what needs to be done?
    We would like to move the styling and javascript to an endpoint or at least add nounce to support Content Security Policy, but that is at least "just" improvements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions