πŸ‘€ Our most exciting product launch yet πŸš€ Join us May 8th for Sanity Connect

Disable new document creation button at structure and global level with document.newDocumentOptions

By Urvashi Bangdel Rai & Herman Wikner

Code examples using document.newDocumentOptions to hide the Create new document button at the structure level and/or disable it in the global create menu.

Hide the Create button at the structure level for all schema types

document: {
	newDocumentOptions: (prev, { currentUser, creationContext }) => {
		if (creationContext.type === 'structure') {
			return [];
		}
		return prev;
	},
},

Hide the Create button at the structure level for a specific schema type

document: {
	newDocumentOptions: (prev, { currentUser, creationContext }) => {
		const { type, schemaType } = creationContext;
		if (type === 'structure' && schemaType == 'pet') {
			return [];
		}
		return prev;
	},
},

Hide the Create button at the structure level for a specific role

document: {
	newDocumentOptions: (prev, { currentUser, creationContext }) => {
		const { type, schemaType } = creationContext;
		if (
			type === 'structure' &&
			currentUser?.roles.find((role) => role.name === 'editor')
		) {
			return [];
		}
		return prev;
	},
},

Disable the global create button for a specific role

document: {
	newDocumentOptions: (prev, { currentUser, creationContext }) => {
		const { type, schemaType } = creationContext;
		if (
			type === 'global' &&
			currentUser?.roles.find((role) => role.name === 'editor')
		) {
			return [];
		}
		return prev;
	},
},

Remove specific document type from the global create menu

document: {
	newDocumentOptions: (prev, { currentUser, creationContext }) => {
		const { type, schemaType } = creationContext;
		if (type === 'global') {
			return prev.filter((template) => template.templateId !== 'pet');
		}
		return prev;
	},
},

Remove specific document type from the global create menu for a specific role

document: {
	newDocumentOptions: (prev, { currentUser, creationContext }) => {
		const { type, schemaType } = creationContext;
		if (
			type === 'global' &&
			currentUser?.roles.find((role) => role.name === 'editor')
		) {
			return prev.filter((template) => template.templateId !== 'pet');
		}
		return prev;
	},
},

document.newDocumentOptions enables configuring templates for new documents that can be reactive to contextual information, such as the currently logged-in user.

This article features code examples that show how to use document.newDocumentOptions to hide the Create new document button at the structure level, or to disable it in the global create menu.

You can show or hide the creation option depending on conditional options such as schema type, current user role, structure/global menu, and so on.

Contributors

Other schemas by authors