🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Configuring a user hierarchy and creating new documents in Sanity

4 replies
Last updated: Mar 20, 2023
I’m setting up a user-configurable hierarchy, and starting to get happy with how it looks now.When I know I’m on the last level I can use
documentTypeList
to get a document list with a “New document” button. But for those cases where there is a mix of folders and documents, how can I get the “New document”-button to appear?
Also, for the newly created document I would like the field for it’s parent to be automatically filled. What is the best way to do this? Can I access the id of the parent folder from the
initialValue
block somehow?
Mar 20, 2023, 9:49 AM
Hmm, this may not be possible, but you can try a few things.First, add an
intent handler method . That may force a create new button to show up but I recall someone mentioning. having issues with it in the past.
You could also try adding a
menu item with a creation action if that doesn’t work.
Mar 20, 2023, 4:13 PM
Thanks,
intent
did the trick!
I could wish for some more examples in the documentation at this point, but I found something in the Community Studio code that I made into this:


S.list()
.title(category.title)
.menuItems(
  [
    {
      title: 'Create new',
      icon: ComposeIcon,
      intent: {
        type: 'create',
        params: {
          type: 'article'
        },
      },
      showAsAction: true
    } 
])
This shows a nice compose icon, and it will create a new article document. It is slightly different behaviour from the default compose button you get from a
documentTypeList
, it doesn’t open the new document in the pane to the right, but on the root level. Any way to force it to open in the next pane?
It might be related the second part of my question, how to open the new document with parent already populated. Is the studio context available in the schema definition, so I can point to the id of the previous pane?
The id is available in the URL, but that feels a bit dirty.
Mar 20, 2023, 6:59 PM
Aha, I found out how to create a new document with parent pre-filled with the category-child template example in the Migrating Initial Value Templates tutorial.
Now I only need to find out how to open the new document in the right pane…
Mar 20, 2023, 8:04 PM
That would be another intent handler! For example:
S.documentList('tag')
                .title('Tags')
                .defaultOrdering([{field: 'title', direction: 'asc'}])
                .menuItems(S.documentTypeList('tag').getMenuItems())
                .filter('_type == $type')
                .params({type: 'tag'})
                .canHandleIntent(S.documentTypeList('tag').getCanHandleIntent())
Mar 20, 2023, 8:10 PM

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?