Client CraftClient Craft HubStart free

Add a public data source to the Visual Builder

On this page
Updated 24 Nov 20263 min read

Implement IVisualDataProvider in your site, register it with AddVisualDataSource<T>() and editors can connect texts and collections to its typed fields.

Plan: Free Visual Builder package

A data source gives editors values your code provides: a service name, opening hours, a list of price options. You describe the source and its typed fields once; the Visual Builder asks you for values per page view, with the site, page, culture and preview state taken from Umbraco's own routing. Your code decides what is public.

Before you start #

  • The Visual Builder is installed, on its own or through the ClientCraft package.
  • Only return public presentation data. Never return customer records, credentials or anything that grants access.

Step by step #

  1. 1
    Describe the source and resolve its valuesAdd a provider class to the website project:
    csharp
    1using ClientCraft.VisualBuilder.Umbraco.Data;
    2
    3namespace MySite.Data;
    4
    5// Public demonstration data only.
    6public sealed class ServiceNameProvider : IVisualDataProvider
    7{
    8 public VisualDataSource Describe() => new(
    9 "agency-public-service",
    10 "Agency public service (example)",
    11 [new VisualDataField("name", "Service name", VisualDataKind.Text)],
    12 IsDemonstration: true);
    13
    14 public Task<VisualDataResult> ResolveAsync(VisualDataContext context, CancellationToken cancellationToken)
    15 {
    16 cancellationToken.ThrowIfCancellationRequested();
    17 var name = context.Culture.StartsWith("pt", StringComparison.OrdinalIgnoreCase)
    18 ? "Manutenção de websites"
    19 : "Website maintenance";
    20
    21 return Task.FromResult(new VisualDataResult(
    22 VisualDataStatus.Success,
    23 new Dictionary<string, VisualDataValue?> { ["name"] = new VisualDataValue.Text(name) }));
    24 }
    25}
  2. 2
    Register it in a composer
    csharp
    1using ClientCraft.VisualBuilder.Umbraco.Data;
    2using Umbraco.Cms.Core.Composing;
    3using Umbraco.Cms.Core.DependencyInjection;
    4
    5namespace MySite.Data;
    6
    7public sealed class DataSourcesComposer : IComposer
    8{
    9 public void Compose(IUmbracoBuilder builder) => builder.AddVisualDataSource<ServiceNameProvider>();
    10}

    AddVisualDataSource<T>() registers the provider per request (scoped). Registering the same class twice is harmless. You can inject your own services into the provider's constructor.

  3. 3
    Build, restart and pick the sourceRestart the site, select a Text block in the builder, switch its Text source to Connect data and choose Agency public service (example) › Service name. Preview the page in English and Portuguese: the text follows the page's culture.

The contract #

TypeMembers
VisualDataSourceKey, Label, Fields, IsDemonstration (default false); IsCollection (init) for a list of records
VisualDataFieldKey, Label, Kind
VisualDataKindText, Number, Money, Date
VisualDataContextSite, Page (keys), Culture, Preview. Built from Umbraco's request, never from authored content
VisualDataValueText(string), Number(decimal), Money(decimal Amount, string Currency), Date(DateOnly)
VisualDataResultStatus, Values; Records (init) for collections; VisualDataResult.Empty, VisualDataResult.Unavailable
VisualDataRecordKey, Values
VisualDataStatusSuccess, Empty, Unavailable, InvalidConfiguration

Rules the builder enforces #

  • Keys. Source keys, field keys and record keys start with a lowercase letter, then use lowercase letters, digits and hyphens, up to 64 characters. No dots.
  • Labels. Up to 120 characters.
  • Sizes. 1 to 64 fields per source, up to 64 sources per site, up to 100 records per collection.
  • Unique keys. Two providers with the same source key stop the data sources from loading. Give every provider its own key.
  • Values must match their kind. A Money value needs a three-letter uppercase currency code such as EUR. A value of the wrong kind makes the result Unavailable.
  • Collections. For a source with IsCollection = true, return the records in Records and leave Values empty.
  • Time. A provider has two seconds. A slow or failing provider is shown as Unavailable; the page still renders and the exception message is not logged (it may contain secrets).

Make real sources safe #

  • Honour the cancellation token, and return VisualDataResult.Empty when there is nothing to show and VisualDataResult.Unavailable when your backend is down.
  • Check context.Preview if your source must not be called from the editor's preview, and check context.Site if it only serves certain sites.
  • Apply your own access rules before returning data. Selecting a source in the editor is not authorization.

What happens next #

Return a list and build a pricing table: Add a pricing table backed by a data source.

Was this article helpful?