Listing documents of a specific schema type in a pane while also listing other document type list items in Slack.

2 replies
Last updated: Jul 17, 2023
Can I list documents of a specific schema type in a pane while also listing some other document type list items?for example: listing all 'person' documents while also having another list item 'directors' in the same list
AI Update

Yes, absolutely! You can combine a documentTypeListItem() (which shows all documents of a type) with other filtered list items in the same pane using the Structure Builder API.

Here's a practical example showing all 'person' documents alongside a filtered 'directors' list:

// ./structure/index.ts

import type {StructureResolver} from 'sanity/structure'

export const structure: StructureResolver = (S) =>
  S.list()
    .title('People')
    .items([
      // All person documents
      S.documentTypeListItem('person').title('All People'),
      
      S.divider(),
      
      // Filtered list showing only directors
      S.listItem()
        .title('Directors')
        .child(
          S.documentList()
            .apiVersion('2024-06-01')
            .title('Directors')
            .schemaType('person')
            .filter('_type == "person" && role == "director"')
        ),
      
      // You could add more filtered lists
      S.listItem()
        .title('Producers')
        .child(
          S.documentList()
            .apiVersion('2024-06-01')
            .title('Producers')
            .schemaType('person')
            .filter('_type == "person" && role == "producer"')
        ),
    ])

The key difference is:

  • documentTypeListItem() - A convenience method that automatically shows all documents of that type
  • listItem() + documentList() - Gives you full control to add filters and create specialized views

You can mix and match these freely in the same items() array. The Structure Builder cheat sheet has great examples of both approaches, and you can read more about dynamically grouping list items with GROQ filters for more complex scenarios.

This pattern is really useful for creating role-based views, status filters, or any other way you want to organize your content while still maintaining access to the complete unfiltered list!

Yep! If you want them mixed:
S.listItem()
        .title('Mixed List')
        .child(
          S.documentList().title('Mixed List').filter(`_type in ['person',
      'director']`)
        ),
Or if you want the directors in a subpane:

S.listItem()
        .title('List with Subpane')
        .child(async () => {
          const client = getClient({ apiVersion: '2023-07-17' });
          const documentListItems = await client
            .fetch(`*[_type == 'actor']`)
            .then(documents =>
              documents.map(document =>
                S.listItem()
                  .title(document.title)
                  .child(S.document().id(document._id).schemaType('actor'))
              )
            );
          return S.list()
            .title('List with Subpane')
            .items([S.documentTypeListItem('director'), ...documentListItems]);
        }),
I was asking for the second solution, I have to try it now. Thank you

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?