Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Displaying unpublished referenced documents in Sanity's desk structure

5 replies
Last updated: Oct 3, 2024
Hello everyone,
I have a question regarding the Desk-Structure and the display of documents in Sanity. I am using a
documentList
in the desk structure that is supposed to display
productModel
documents. The problem is that these documents are not displayed if they reference a product document that is not published.
Here is my current code for the `documentList`:


S.listItem()
  .title('Models')
  .icon(RocketIcon)
  .child(
    S.documentList()
      .apiVersion('v2023-11-17')
      .title('Models')
      .filter(
        '_type == "productModel" && references(*[_type=="product" && productType == "series"]._id)'
      )
  )
As soon as the
product
document is published, the corresponding
productModel
documents appear in the list. Is there a way to display the
productModel
documents in the documentList even if the referenced
product
document is not yet published?
I have already tried to extend the query to include drafts, but that doesn't seem to work.
Do any of you have an idea how I can solve this problem?

I appreciate any tips!
Oct 2, 2024, 6:41 PM
I would assume its the
._id
in the end of the filter that is the problem here, what happens if you remove that?
Also what happens if you change
documentList
to
documentTypeList
?
Oct 2, 2024, 8:58 PM
A reference field in a document will always contain the published
_id
of said document (unless you’re using weak references). A draft of a document is just a copy of the published version with the
_id
prefixed with
drafts.
. This is responsible for the mismatch in behavior you’re getting.
I also didn’t think that projections inside of filters/the
references
function worked! This is how I would approach it:
import {map} from 'rxjs'
export const structure = (S, context) =>
  S.list()
    .title('Content')
    .items([
      S.listItem()
        .title('Models')
        .child(() =>
          context.documentStore
            .listenQuery(
              `*[_type == 'movie' && productType == 'series']{
                  'screening': *[_type == 'screening' && references(string::split(^._id, '.')[1]) || references(^._id)],
              }.screening[]._id`,
            )
            .pipe(
              map((ids) => {
                return S.documentList().title('Models').filter(`_id in $ids`).params({ids})
              }),
            ),
        ),
    ])
Note that in this example I’m using the Movie DB database that you can bootstrap via the CLI.
Oct 2, 2024, 9:21 PM
Also I currently have a wicked cold, so hopefully this made sense 😅
Oct 2, 2024, 9:22 PM
thanks that works 😍
i just had to adjust the condition because otherwise documents of other types would have ended up in the list.


.listenQuery(
  `*[_type == 'movie' && productType == 'series']{'screening': *[_type == 'screening' && references(string::split(^._id, '.')[1]) || _type == 'screening' && references(^._id)],
  }.screening[]._id`,
)
get well soon
Oct 3, 2024, 6:43 AM
Excellent!
Oct 3, 2024, 2:31 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?