Building a custom dashboard widget to display data within Sanity Studio using groq queries.

10 replies
Last updated: Jul 29, 2022
Hi there, I'm looking to build a dashboard widget to display some content within the sanity studio of a website I'm building, rather than the front end.I'm looking for some boilerplate code where I can run a groq query and then output some data. A simple example could be fetch all of the titles of all of the posts and display them inside of an unordered list within the dashboard widget. Is anyone able to point me in the right direction?
AI Update

Great question! You're looking to build a custom dashboard widget in Sanity Studio. Here's how to get started:

Using the Example Dashboard Widget

The best approach is to use the example-dashboard-widget-document-count as your boilerplate. This is an official Sanity example that shows exactly how to run GROQ queries and display data in a dashboard widget.

Basic Setup

First, you'll need to install the dashboard plugin:

npm install @sanity/dashboard

Then configure it in your sanity.config.ts (or sanity.config.js):

import {defineConfig} from 'sanity'
import {dashboardTool} from '@sanity/dashboard'

export default defineConfig({
  // ... other config
  plugins: [
    dashboardTool({
      widgets: [
        // Your custom widgets go here
      ]
    })
  ]
})

Creating Your Custom Widget

For your use case (listing all post titles), you'd create a custom widget component. Here's a simple example structure:

import React, {useEffect, useState} from 'react'
import {Card, Stack, Text} from '@sanity/ui'
import {useClient} from 'sanity'

export function PostListWidget() {
  const client = useClient({apiVersion: '2023-01-01'})
  const [posts, setPosts] = useState([])

  useEffect(() => {
    // Your GROQ query - fetch all post titles
    client.fetch('*[_type == "post"]{title}')
      .then(data => setPosts(data))
  }, [client])

  return (
    <Card padding={4}>
      <Stack space={3}>
        <Text size={2} weight="bold">All Posts</Text>
        <ul>
          {posts.map((post, index) => (
            <li key={index}>{post.title}</li>
          ))}
        </ul>
      </Stack>
    </Card>
  )
}

The key parts here are:

  • useClient hook: This gives you access to the Sanity client to run GROQ queries within Studio
  • GROQ query: Replace *[_type == "post"]{title} with whatever query fits your needs
  • Sanity UI: Use these components to style your widget consistently with Studio

Important Resources

The example widget uses simple GROQ methods like count(*), but you can use any GROQ query you need for more complex data fetching and transformations.

Which language? So you want to build this outside of the Sanity Studio, right?
Javascript - I am looking to build a dashboard widget to show some data - So within Sanity studio like in the screenshot below. So really just some boilerplate code of how to run a basic groq query and display some data it in this location
I don't know of any, but take a look at this one (or any of the code shared through the Sanity exchange: https://www.sanity.io/plugins/documents-pane
Great, I'll take a look thanks 🙂
user L
is right, that this will work within the desk as a view (so it’s a plugin for the structure builder), but if you want something for the Dashboard, you could try this here: https://www.sanity.io/plugins/sanity-plugin-dashboard-widget-document-list 🙂
Hi
user J
thanks for the message. I've looked into the document list plugin - as far as I know it doesn't look like it will provide what I'm looking for here. I'm looking for something a bit more custom.
To provide a bit more context I have a sanity powered website where users can confirm if they are going to an event or not. So users can view their invitation and submit a form which posts an 'rsvp' with either 'yes' or 'no' selected and also the names and guests they will be brining as well as some other details such as special dietary requirements, if they are interested in certain transport options. So I'm looking to build a widget which maps over all of the RSVPs and output the current list of guests attending - it would be great for an MVP just to list the total number of guests who have said they're going to attend the event - but I would like to expand on it further - such as to display an unordered list all the names or maybe a table of all the important data I'll need as part for the admin side of the event.

So I'm looking for a way to output all of this data somewhere inside of the studio - it definitely doesn't need to be the dashboard but I figured that might be a good place. If anyone can figure out what I'm trying to do and suggest an approach that could get the job done I'm really interested.
Well in that case, you would have to write a custom widget yourself, but you could checkout this existing one and use it’s code as a boilerplate.Use a more in depth query for your needs instead of
count(*)
and our wonderful Sanity UI and you should be able to do so pretty quicklyWhat do you think?
Thanks
user J
- glad you clarified that
count(*)
was the query - I think I previously looked at this plugin and overlooked that! This is exactly what I need. Thanks again for your help 😀
Wonderful! (and yes, not everyone can spot the GROQ methods and a hint can make the difference 🙂)You might consider sharing your code later on our
exchange and help others with similar concerns!

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?