For the second part (inserting org-agenda) this is what I came up with, I wonder if there is another way?
Using org-version 9.3.6. (updated slightly) for org-version 9.4.4
(defun org-dblock-write:sjm/org-insert-agenda (params)
"Writes agenda items with some some text from the entry as context
to dynamic block.
If key is a string of length 1, it is used as a key in
`org-agenda-custom-commands` and triggers that command. If it
is a longer string it is used as a tags/todo match string.
String to insert before context text. Defaults to two spaces \" \".
Do not use asterisk \"* \".
Maximum number of lines to include, defaults to
Regex to replace. Defaults to heading asterisk characters and
first uppercase word (TODO label): \"^\\* [A-Z-]* \"
String to replace the org-heading star with.
Defaults to \"- \" such that headings become list items.
Somewhat adapted from org-batch-agenda.
(cmd-key (or (plist-get params :key) "b"))
(org-agenda-entry-text-leaders (or (plist-get params :leaders) " "))
(org-agenda-entry-text-maxlines (or (plist-get params :count)
(replace-this (or (plist-get params :repalce-this) "^\\* [A-Z-]* "))
(replace-with (or (plist-get params :replace-with) "- "))
(save-window-excursion ; Return to current buffer and window when done.
(if (> (length cmd-key) 1) ; If key is more than one character, THEN
(org-tags-view nil cmd-key) ; Invoke tags view, ELSE
(org-agenda nil cmd-key)) ; Invoke agenda view using key provided.
(setq data (buffer-string)) ; copy agenda buffer contents to data
(with-temp-buffer ; Using a temporary buffer to manipulate text.
(insert data) ; place agenda data into buffer.
(goto-char (point-max)) ; end-of-buffer
(beginning-of-line 1) ; beggining of last line.
(while (not (bobp)) ; while not begging of buffer
(when (org-get-at-bol 'org-hd-marker) ; get text property.
(sjm/org-agenda-entry-text)) ; Insert item context underneath.
(beginning-of-line 0)) ; Go to previous line
(setq data (buffer-string)))) ; Copy buffer, close tmp buf & excursion.
;; Paste data, replacing asterisk as per replace-this with replace-with.
(insert (replace-regexp-in-string replace-this replace-with data))))
(defun sjm/org-agenda-entry-text ()
"Insert some text from the current agenda item as context.
Adapted from `org-agenda-entry-text-show-here', relies upon
`org-agenda-get-some-entry-text' for the bulk of the work."
(save-excursion ; return to current place in buffer.
(let (m txt o) ; declare some local variables.
(setq m (org-get-at-bol 'org-hd-marker)) ; get text property
(unless (marker-buffer m) ; get buffer that marker points into.
(error "No marker points to an entry here"))
;; get some entry text, remove any properties and append a new-line.
(setq txt (concat "\n" (org-no-properties
(when (string-match "\\S-" txt)
(insert txt "\n\n")))))
org-agenda is then applied using
C-c -C-x C-u while within the following block
#+BEGIN: sjm/org-insert-agenda :key "b"
I've not really looked at the first part yet.