How to fix "untitled" document name in Sanity schema type?

15 replies
Last updated: Jul 12, 2021
Hello - I added a schema type for 'content' and whenever I 'create new' it shows up as 'untitled' and there seems no way to change the name- any idea what I'm doing wrong?
AI Update

The issue you're seeing is because your schema doesn't have a preview configuration set up! When Sanity doesn't know which field to use as the document title, it defaults to showing "untitled" everywhere.

Here's how to fix it - you need to add a preview configuration to your 'content' schema type. This tells Sanity which field to display as the title:

export default {
  name: 'content',
  type: 'document',
  fields: [
    {
      name: 'title',
      type: 'string',
      title: 'Title'
    },
    // your other fields...
  ],
  preview: {
    select: {
      title: 'title'  // This tells Sanity to use the 'title' field
    }
  }
}

The preview configuration uses two parts:

  • select: Specifies which fields to pull from your document
  • prepare (optional): Lets you transform the data before display

If you want to get fancier, you can add subtitles and media too:

preview: {
  select: {
    title: 'title',
    subtitle: 'description',
    media: 'coverImage'
  }
}

Or use the prepare function for custom formatting:

preview: {
  select: {
    title: 'title',
    publishedAt: 'publishedAt'
  },
  prepare({title, publishedAt}) {
    return {
      title: title,
      subtitle: publishedAt ? `Published: ${publishedAt}` : 'Draft'
    }
  }
}

Once you add this to your schema and save, your documents should show their actual title instead of "untitled" in lists, references, and search results!

Show original thread
15 replies
hey! can you share your content schema?
(from a structured content point of view, I think that “content” is too generic for a document type, but that’s beside the point.)
Sanity tries to pick the most suitable field as a title. If it can’t find any, you need to manually specify the field.
export default {
  name: 'content',
  title: 'Content',
  type: 'document',
  icon,
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'string',
    },
    {
      name: 'slug',
      title: 'Slug',
      type: 'slug',
      options: {
        source: 'title',
        maxLength: 100,
      },
    },
    {
      name: 'metaTitle',
      title: 'Meta Title',
      type: 'string',
    },
    {
      name: 'metaDesc',
      title: 'Meta Description',
      type: 'string',
    },
    {
      name: 'content',
      title: 'Content',
      type: 'blockContent',
    },
    {
      name: 'dateCreated',
      title: 'Date Created',
      type: 'datetime',
      initialValue: (new Date()).toISOString(),
    },
    {
      name: 'image',
      title: 'Main/social Image',
      type: 'image',
      options: {
        hotspot: true,
      },
    },
    {
      name: 'skills',
      title: 'Skills',
      type: 'array',
      of: [{type: 'reference', to: {type: 'skill'}}],
      options: {
        layout: 'tags'
      }
    },
    {
      name: 'grades',
      title: 'Grades',
      type: 'array',
      of: [{type: 'reference', to: {type: 'grade'}}],
      options: {
        layout: 'tags'
      }
    },
    {
      name: 'contentType',
      title: 'Content Type',
      type: 'array',
      of: [{type: 'string'}],
      options: {
        list: [
          {title: 'skill', value: 'skill'},
          {title: 'game', value: 'game'},
        ]
      }
    },
  ],
Interesting. The studio should definitely snag the value of
title
and use it in your preview (i.e., as the title of the document in the list). Are you setting a preview later in your schema or is there anything else in your schema that you didn’t paste here? I was able to get your code working with just a few changes (renaming the document name, removing icon, and setting the reference types to document types I already have in my schema).
I have a preview..
Interesting. The studio should definitely snag the value of
title
and use it in your preview (i.e., as the title of the document in the list). Are you setting a preview later in your schema or is there anything else in your schema that you didn’t paste here? I was able to get your code working with just a few changes (renaming the document name, removing icon, and setting the reference types to document types I already have in my schema).
the preview doesn't work in the ui, not sure where I went wrong... could that be causing the untitled?
preview: {
    select: {
      title: 'title',
      content: 'content',
      date: 'dateCreated',
      main: 'image',
      grades: 'grades',
      skills: 'skills',
    },
    prepare(selection) {
      const gradesStr = selection.grades.filter(Boolean).join(', ')
      const skillsStr = selection.skills.filter(Boolean).join(', ')

      return {
        title: `${selection.title} ${year ? `(${year})` : ''}`,
        content: selection.content,
        date: selection.date,
        skills: selection.skills,
        skillsStr: skillsStr,
        grades: selection.grades,
        gradesStr: gradesStr,
        mainImage: selection.main,
      }
    },
  },
(copied from the movie example... didn't edit that part yet, hence the year)
Do you have a link to that example? I don’t recall seeing a
prepare()
that returns more than a few properties (title, subtitle, description, media, and perhaps one or two I’m missing).
In any case, I suspect it’s an error in your preview causing you to see Untitled (I’m guessing you see
Invalid preview config at the top of your document).
yes
okay, I'll get the preview working and see if that works
ya, that did it
Great! If you face any issues getting the functionality you want in your preview, please follow up.

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.

Was this answer helpful?