🎤 Builder Talk: The Story Behind Lady Gaga’s Digital Experience – Register now

Is it possible to mix explicitly defined list items with a dynamic documentList in the structure builder?

3 replies
Last updated: Jun 10, 2021
In the structure builder, is it possible to mix explicitly defined list items with a dynamic documentList?For example showing a list item at the top of the pane for categories, and then the list of all posts below, instead of having to drill down one more level to "all posts".
Jun 7, 2021, 1:57 AM
Wondering the same thing!
Jun 9, 2021, 11:45 AM
User I think it may be possible by defining the structure asynchronously , and then manually creating an array of items with the categories one first, and the document items following, instead of using a built in structure builder method. Here's something similar I did:
S.listItem()
  .title('Child Pages by Parent')
  .icon(RiPagesLine)
  .child(() =>
    documentStore.listenQuery(`*[_type == "page" && defined(content.main.parent._ref)]{'parent': content.main.parent->}`).pipe(
      map(allParentPages => {
        // Filter array for unique values since the query will return
        // the parents of all children so there will be duplicates
        const parents = allParentPages
          .filter(
            (parentDocument, index, array) =>
              array
                .map(mapObj => mapObj._id)
                .indexOf(parentDocument._id) === index,
          )
          .map(uniqueParents => uniqueParents.parent);

        const parentListItems = parents.map(parent =>
          S.listItem()
            .title(parent.content.main.title)
            .icon(RiPagesLine)
            .child(
              S.documentList()
                .title(parent.content.main.title)
                .schemaType('page')
                .filter('content.main.parent._ref == $parentId')
                .params({
                  parentId: parent._id,
                })
                .child(S.document().views(Views())),
            ),
        );

        return S.list()
          .title('Child Pages by Parent')
          .items(parentListItems);
      }),
    ),
  ),
Jun 9, 2021, 4:57 PM
Thank You Nic! Will give this a try đź‘Ť
Jun 10, 2021, 7:57 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
Custom Document Views - Is it Even Possible to Use the Structure Builder?Apr 21, 2022
Organizing document types in Sanity Studio using Structure BuilderMay 13, 2020
How to structure the content on the front page of a website using Sanity.io.Jun 19, 2020
Request for "generate" button for string type and suggestion to use slug schema type instead.Jul 29, 2020
Trouble querying a field with array of references in Slack threadSep 1, 2020
any hints on creating a group of singletons in structure builder?Jan 14, 2021
How to change the title of a document in Sanity's structure builder and how to make a schema type a singleton.May 13, 2020
Querying unique values in GROQ and creating a documentList in SanityJun 9, 2020
Discussion about resolving an error in setting up a custom desk structure in Sanity.ioMar 26, 2021
Discussion about date field changes and using emojis as icons in Sanity.ioApr 2, 2021

Related contributions

Turbo Start Sanity
- Template

The battle-tested Sanity template that powers Roboto Studio's websites

Go to Turbo Start Sanity

Schema UI - Next.js Sanity Starter
- Template

A Next.js starter template with Next.js 15, Tailwind CSS, shadcn/ui, and Sanity CMS with Live Editing. Get production-ready React components with matching Sanity schemas and queries. Build dynamic pages faster while keeping full control over customization.

Serge Ovcharenko
Go to Schema UI - Next.js Sanity Starter

The Swaddle
- Made with Sanity

A new brand identity to represent a more mature company, to signify The Swaddle’s evolution from publisher to production house, combined with an easier to navigate platform that can surface multiple content types - drawing readers through The Swaddle’s content offering.

Nightjar
Go to The Swaddle