Add a public data source to the Visual Builder
On this page
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 #
- 1Describe the source and resolve its valuesAdd a provider class to the website project:1using ClientCraft.VisualBuilder.Umbraco.Data;23namespace MySite.Data;45// Public demonstration data only.6public sealed class ServiceNameProvider : IVisualDataProvider7{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);1314 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";2021 return Task.FromResult(new VisualDataResult(22 VisualDataStatus.Success,23 new Dictionary<string, VisualDataValue?> { ["name"] = new VisualDataValue.Text(name) }));24 }25}
- 2Register it in a composer1using ClientCraft.VisualBuilder.Umbraco.Data;2using Umbraco.Cms.Core.Composing;3using Umbraco.Cms.Core.DependencyInjection;45namespace MySite.Data;67public sealed class DataSourcesComposer : IComposer8{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. - 3Build, 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 #
| Type | Members |
|---|---|
VisualDataSource | Key, Label, Fields, IsDemonstration (default false); IsCollection (init) for a list of records |
VisualDataField | Key, Label, Kind |
VisualDataKind | Text, Number, Money, Date |
VisualDataContext | Site, Page (keys), Culture, Preview. Built from Umbraco's request, never from authored content |
VisualDataValue | Text(string), Number(decimal), Money(decimal Amount, string Currency), Date(DateOnly) |
VisualDataResult | Status, Values; Records (init) for collections; VisualDataResult.Empty, VisualDataResult.Unavailable |
VisualDataRecord | Key, Values |
VisualDataStatus | Success, 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
Moneyvalue needs a three-letter uppercase currency code such asEUR. A value of the wrong kind makes the result Unavailable. - Collections. For a source with
IsCollection = true, return the records inRecordsand leaveValuesempty. - 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.Emptywhen there is nothing to show andVisualDataResult.Unavailablewhen your backend is down. - Check
context.Previewif your source must not be called from the editor's preview, and checkcontext.Siteif 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?