Sanity App SDK
    Preparing search index...

    Function useDocumentPermissions

    • Check if the current user has the specified permissions for the given document actions.

      Parameters

      Returns DocumentPermissionsResult

      An object that specifies whether the action is allowed; if the action is not allowed, an explanatory message and list of reasons is also provided.

      When passing multiple actions, all actions must belong to the same project and dataset. Note, however, that you can check permissions on multiple documents from the same project and dataset (as in the second example below).

      import {
      useDocumentPermissions,
      useApplyDocumentActions,
      publishDocument,
      createDocumentHandle,
      type DocumentHandle
      } from '@sanity/sdk-react'

      // Define props using the DocumentHandle type
      interface PublishButtonProps {
      doc: DocumentHandle
      }

      function PublishButton({doc}: PublishButtonProps) {
      const publishAction = publishDocument(doc)

      // Pass the same action call to check permissions
      const publishPermissions = useDocumentPermissions(publishAction)
      const apply = useApplyDocumentActions()

      return (
      <>
      <button
      disabled={!publishPermissions.allowed}
      // Pass the same action call to apply the action
      onClick={() => apply(publishAction)}
      popoverTarget={`${publishPermissions.allowed ? undefined : 'publishButtonPopover'}`}
      >
      Publish
      </button>
      {!publishPermissions.allowed && (
      <div popover id="publishButtonPopover">
      {publishPermissions.message}
      </div>
      )}
      </>
      )
      }

      // Usage:
      // const doc = createDocumentHandle({ documentId: 'doc1', documentType: 'myType' })
      // <PublishButton doc={doc} />
      import {
      useDocumentPermissions,
      editDocument,
      type DocumentHandle
      } from '@sanity/sdk-react'

      export default function canEditMultiple(docHandles: DocumentHandle[]) {
      // Create an array containing an editDocument action for each of the document handles
      const editActions = docHandles.map(doc => editDocument(doc))

      // Return the result of checking for edit permissions on all of the document handles
      return useDocumentPermissions(editActions)
      }