# V2. Org-roam capture from code

**URL:** <https://org-roam.discourse.group/t/v2-org-roam-capture-from-code/1737>\
**Category:** Development\
**Created:** [July 7, 2021, 11:15am UTC](https://org-roam.discourse.group/t/v2-org-roam-capture-from-code/1737 "2021-07-07T11:15:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![agibragimov](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/agibragimov/32/589_2.png) [@agibragimov](https://org-roam.discourse.group/u/agibragimov)\
**Post date:** [July 7, 2021, 11:15am UTC](https://org-roam.discourse.group/t/v2-org-roam-capture-from-code/1737/1 "2021-07-07T11:15:32Z")

</div>

I need from time to time to convert an Org Heading to a file-note. With v1 of Org-roam I used to do it using a custom function that eventually calls `(org-roam-capture--capture)`.

That function was removed from v2 and the org-roam capture mechanism has been changed.

Can someone share a snippet or give me some pointers how to rewrite my old function and make it compatible with v2?

---

<div class="post-metadata">

**Author:** ![nobiot](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/nobiot/32/159_2.png) [@nobiot](https://org-roam.discourse.group/u/nobiot)\
**Post date:** [July 7, 2021, 12:58pm UTC](https://org-roam.discourse.group/t/v2-org-roam-capture-from-code/1737/2 "2021-07-07T12:58:32Z")

</div>

A very simple way would be to create a function to work on the current subtree to do something like this:

1. Narrow to subtree `org-narrow-to-subtree`
2. Create a new buffer
3. Insert the entire subtree to the new buffer
4. Save the buffer with `.org` extension

Not sure if there is anything more you would like to do, but you could also add the following rather easily:

- Use the current subtree’s heading (title) as the new file’s title  
e.g. At point 1 of the narrowed buffer, `org-element-at-point` and `org-element-property` to get the headline’s title and insert it to as `#+title: <insert the text here>` – Simply use `format` or `concat`
- Generate a new Org-ID for the file  
Simply use `org-id-get-create`

Just a quick idea.

---

<div class="post-metadata">

**Author:** ![agibragimov](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/agibragimov/32/589_2.png) [@agibragimov](https://org-roam.discourse.group/u/agibragimov)\
**Post date:** [July 7, 2021, 7:30pm UTC](https://org-roam.discourse.group/t/v2-org-roam-capture-from-code/1737/3 "2021-07-07T19:30:58Z")

</div>

That makes sense. However, this won’t protect from accidentally creating a note file with the same title multiple times. Besides, using org-roam-capture’s machinery would allow creating new file-notes exactly as stated in the template.

And also opens the possibility for implementing the reverse - re-filing the entire content of a file-note as a subheading to another node (with subsequent deletion of the file). For example, no need to keep whole file titled “Taxonomy”, if it can easily be just a heading in a file-note for “Biology”.

---

<div class="post-metadata">

**Author:** ![nobiot](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/nobiot/32/159_2.png) [@nobiot](https://org-roam.discourse.group/u/nobiot)\
**Post date:** [July 7, 2021, 10:24pm UTC](https://org-roam.discourse.group/t/v2-org-roam-capture-from-code/1737/4 "2021-07-07T22:24:21Z")

</div>

Sure; it’s up to you how you would like to craft it.

---

<div class="post-metadata">

**Author:** ![nobiot](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/nobiot/32/159_2.png) [@nobiot](https://org-roam.discourse.group/u/nobiot)\
**Post date:** [July 8, 2021, 9:51am UTC](https://org-roam.discourse.group/t/v2-org-roam-capture-from-code/1737/5 "2021-07-08T09:51:46Z")

</div>

What’s the reason why you can’t use `org-roam-capture` on V2?

---

<div class="post-metadata">

**Author:** ![agibragimov](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/agibragimov/32/589_2.png) [@agibragimov](https://org-roam.discourse.group/u/agibragimov)\
**Post date:** [July 21, 2021, 8:57pm UTC](https://org-roam.discourse.group/t/v2-org-roam-capture-from-code/1737/6 "2021-07-21T20:57:10Z")

</div>

Finally, I had some time to dig around the issue and wrote this:

> <https://gist.github.com/agzam/607acb518f584e0ae6b902697dc53b45>

Currently, it only knows how to convert a Heading to a separate file-note. At some point I’d like to extend this, so it works the other way around too. And I also would like to be able to “re-file” content, moving it from one node to another.

btw: I opened an issue to track this: [Org-roam-refile · Issue #1658 · org-roam/org-roam · GitHub](https://github.com/org-roam/org-roam/issues/1658)
