siyuan-note/siyuan · error

sort target ID is required when sort position is specified

Error message

sort target ID is required when sort position is specified

What it means

createDocByMd refuses to create a document when a sortPosition (e.g. 'top', 'bottom', or an index) is given but no sortTargetID identifies the sibling document to position against. SiYuan cannot order the new document without a concrete target document. This is a pure API misuse guard, not a runtime fault.

Solutions

  1. Pass a valid sibling document ID in sortTargetID whenever sortPosition is set
  2. Omit sortPosition entirely to append the document normally
  3. Verify the target exists and is a sibling first via GetBlockTree / listDocs

Example fix

// before
fetchPost('/api/filetree/createDocWithMd', {notebook: boxID, path: p, markdown: md, sortPosition: 'top'})
// after
fetchPost('/api/filetree/createDocWithMd', {notebook: boxID, path: p, markdown: md, sortPosition: 'top', sortTargetID: siblingDocID})
Defensive patterns

Strategy: validation

Validate before calling

if (sortPosition && !sortTargetID) throw new Error('sortTargetID is required when sortPosition is set')

Try / catch

try { await api.createDocWithMd(args) } catch (e) { if (String(e).includes('sort target ID is required')) { /* retry without sortPosition */ } }

Prevention

When it happens

Trigger: Calling CreateDocByMd/CreateDocByMdSync (or /api/filetree/createDocWithMd-family endpoints) with a non-empty sortPosition but empty or whitespace sortTargetID.

Common situations: Plugin or script code that builds the request payload conditionally and fills sortPosition but forgets sortTargetID; copy-pasted API snippets that hardcode 'sortPosition':'top' without 'sortTargetID'.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/7f6f597307af00ca. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/file.go:1308

	createDocLock.Lock()
	defer createDocLock.Unlock()

	box, err := getOpenedBox(boxID)
	if nil != err {
		return
	}
	sortTargetID, _ := arg["sortTargetID"].(string)
	sortPosition, _ := arg["sortPosition"].(string)
	if "" != sortTargetID {
		if "before" != sortPosition && "after" != sortPosition {
			return nil, fmt.Errorf("invalid sort position [%s]", sortPosition)
		}
		target := treenode.GetBlockTree(sortTargetID)
		if !isSortableDocument(target) || target.BoxID != boxID || path.Dir(target.Path) != path.Dir(p) {
			return nil, fmt.Errorf("sort target document [%s] is not a sibling of the new document", sortTargetID)
		}
	} else if "" != sortPosition {
		return nil, errors.New("sort target ID is required when sort position is specified")
	}

	luteEngine := util.NewLute()
	luteEngine.SetHTMLTag2TextMark(true)
	dom := luteEngine.Md2BlockDOM(md, false)
	tree, err = createDoc0(box.ID, p, title, dom, false, syncWrite)
	if err != nil {
		return
	}

	FlushTxQueue()
	if "" != sortTargetID {
		if _, sortErr := ReorderDocs([]string{tree.ID}, sortTargetID, sortPosition); nil != sortErr {
			logging.LogErrorf("reorder created document [%s] failed: %s", tree.ID, sortErr)
			box.setSortByConf(path.Dir(tree.Path), tree.ID)
		}
	} else if 0 < len(sorts) {
		ChangeFileTreeSort(box.ID, sorts)

View on GitHub (pinned to 9f775e8a12)