How does the "update existing entries" feature in CSV Import work?

How does the "update existing entries" feature in CSV Import work?

by TJ Meneses -
Number of replies: 1

I'm currently trying to do a simple update using CSV Import: one entry, only a few fields changed, and using the username as the key. Only when I import it, a new entry is created. I've tried replacing EAU:username with EAU:id, and the result is the same.

How then should I upload the CSV such that it updates the existing entries instead of making a new one?

Average of ratings: Useful (1)
In reply to TJ Meneses

Re: How does the "update existing entries" feature in CSV Import work?

by TJ Meneses -

A pre-amble:

  • I discovered the solution by myself by sifting through the source code, which I will lay out in this post.
  • It's also astoundingly undocumented, either in any of the online docs or anywhere in the forum. Let this serve as a helpful guide for anyone trying to use the CSV Import function to update their existing entries.
  • Finally, implementing this solution triggers the "suspicious patterns found" warning. As far as I can tell though, everything else is working as intended. Let this serve as an unofficial bug report: a false positive for the pattern validation.

Now, on to the fun bits. The critical piece of the puzzle here is in this field pattern: entryid. The pattern pulls the internal ID of the actual entry. It must be part of the entry template in the CSV view. I haven't tested whether it has to be the first column, but that's where I declared it, and it's good practice to put a record's key anyway.

Keep in mind that the entry ID is a system generated one, so in order to update any existing entry, one must first export the entries to be updated. (Note: there's a bug where the exported CSV randomly adds a space before some commas; I resolve this by doing a "Find & Replace" on [space], to ,.)

Once you have the CSV file, feel free to delete all rows and columns that won't be updated; just remember to retain the entryid column. Upload the file (or copy-paste the text) in the CSV Import page, set "Update existing entries" to Yes, and click Import. And you're done!

One final note: updating entries this way also updates the timemodified field. I haven't checked if including that column in the imported CSV file will suppress this behavior because it's what I want in my case. My gut instinct would be yes, but as with anything, test it out first in a separate Moodle environment.

Hope this post has been easy to follow and is helpful to others. While I'm glad that I got the update feature to work, it would have been a much better use of my time if it was just documented in the first place. With luck, others with similar needs will stumble upon this post in their search, so their time is saved.