Exporting to CSV in Modx

Sometimes the easiest way to bulk edit or export data is through a csv or excel file. While its easy enough to export a database table as CSV if your exporting from Modx then it’s not that straight forward.

The reason for this is due to the way Modx stores Template Variables – within a table that references the page the TV belongs to and the type of variable it is by ID number. As such you have to join or cross reference at least 3 tables. I get why Modx is built this way, it is after all the most effective way of using a relional database structure but if you’ve got a few thousand pagesĀ  with 10 – 20 template variables per page it becomes a big task to sort the data.

Looking around the web there isn’t much support on exporting Modx data, the ever helpful Bob suggests building a plugin (which seems a little restrictive and cumbersome). There is a tool out there that will export the data, however the owner charges $20 just to install on one site – which seems against the nature of the open source CMS.

The fact is all we really need it to use is the most useful plugin made for Modx – Get Resources. All you need to do is create a new page with a blank template, change the page type from HTML to text and then call Get resources within the [[*content]] like so:

[[getResources? &tpl=`csvlist` &parents=`692` &includeContent=`1` &includeTVs=`1` &tvPrefix=`` &limit=`0` &showHidden=`1`]]

For the chunk we then enclose each template variable within speechmarks and seperated with a comma:

"[[+pagetitle]]","[[+menutitle]]","[[+description]]","0","[[+introtext]]","[[~[[+id]]]]"

Publish and save the page then view it in the browser and you will be able to save the page as txt or csv and view it in any spreadsheet or editing program.

The beaty of this approach is you can choose exactly which TV’s to include while all Modx tags are also outputted correctly.