How to get the title field from a reference field in Sanity.io
Yes! You can access the title from your referenced form document using the select property in your preview configuration. Here's how to update your code:
{
name: 'Forms',
type: 'object',
preview: {
select: {
formTitle: 'formBuilder.title', // Access the title field from the referenced form
},
prepare: ({formTitle}) => ({
title: formTitle || 'Form Builder', // Use the form's title, with fallback
subtitle: 'Form' // Optional: add a subtitle for context
}),
},
fields: [
{
title: 'Forms',
name: 'formBuilder',
type: 'reference',
to: [{ type: 'form' }],
},
],
}The key is using dot notation in the select object: formBuilder.title tells Sanity to follow the reference and grab the title field from the referenced form document. Then in the prepare function, you can use that value to display it as the preview title.
You can access any field from the referenced document this way, not just the title. For example, if your form had other fields like description or formType, you could access them with formBuilder.description or formBuilder.formType.
This is explained in the preview configuration documentation, which shows how to access fields from referenced documents using dot notation in the select object.
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.