
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeI understand you want to automatically create user documents for all project members each time the Studio loads. Unfortunately, the links you referenced appear to be broken or moved, but I can help you solve this problem.
The challenge you're facing is that certain hooks (like the one that might create initial documents) don't work with custom desk structures. Here are a few approaches to solve this:
Instead of relying on desk structure lifecycle, create a Studio plugin that runs initialization logic when the Studio mounts:
// plugins/userDocumentSync.js
import {definePlugin} from 'sanity'
import {useEffect} from 'react'
export const userDocumentSync = definePlugin({
name: 'user-document-sync',
studio: {
components: {
layout: (props) => {
useEffect(() => {
// Fetch project members and sync user documents
syncUserDocuments()
}, [])
return props.renderDefault(props)
}
}
}
})
async function syncUserDocuments() {
const client = sanityClient.withConfig({apiVersion: '2024-01-01'})
// Fetch project members using Management API
const members = await fetch(
`https://api.sanity.io/v2021-06-07/projects/${projectId}/members`,
{headers: {Authorization: `Bearer ${token}`}}
).then(res => res.json())
// Create/update documents for each member
for (const member of members) {
await client.createIfNotExists({
_id: `user.${member.id}`,
_type: 'user',
name: member.displayName,
email: member.email
})
}
}Then add this plugin to your sanity.config.js:
import {userDocumentSync} from './plugins/userDocumentSync'
export default defineConfig({
// ...
plugins: [
// ... other plugins
userDocumentSync()
]
})For a more robust solution that doesn't depend on Studio loading, use Sanity Functions with a scheduled trigger:
// sanity.blueprint.ts
import {defineBlueprint} from '@sanity/blueprint'
export default defineBlueprint({
functions: [
{
name: 'sync-user-documents',
runtime: 'node22',
schedule: '0 * * * *', // Run hourly
handler: async (event) => {
// Fetch members from Management API
// Sync to dataset
}
}
]
})This approach is better because:
Add a manual sync action to the Studio's tool menu that users can trigger when needed:
// tools/syncUsers.js
import {definePlugin} from 'sanity'
export const syncUsersTool = definePlugin({
name: 'sync-users-tool',
tools: [
{
name: 'sync-users',
title: 'Sync Users',
component: SyncUsersComponent
}
]
})https://api.sanity.io/v2021-06-07/projects/{projectId}/memberscreateIfNotExists() to avoid duplicatesThe plugin with layout component wrapper is your best bet for the "run every time Studio loads" requirement with a custom desk structure. It works by wrapping the entire Studio layout rather than hooking into the desk structure specifically.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store