Batch-adding Users to Plone using CSV (Excel) files
by
Wyden Silvan
—
last modified
18.11.2009 13:58
- Create a textfile with your userdata. The format is:
password;userid;lastname;firstname;email
- Log into the ZMI of your site and create a 'File' instance at the root of your plone instance. Give it the id 'users.csv' and the file you just created:
- Copy the script:
# Create new members with properties supplied from a CSV file.
# The script expects a File object with id `users.csv` in the same folder
# it resides.
#
# The format of the CSV needs to be:
#
# password;userid;lastname;firstname;email
#
# created 2006-11-03 by Tom Lazar <tom@tomster.org>, http://tomster.org/
# under a BSD-style licence (i.e. use as you wish but don't sue me)
from Products.CMFCore.utils import getToolByName
users = context['users.csv'].data.split('\n')
regtool = getToolByName(context, 'portal_registration')
index = 1
imported_count = 0
for user in users:
tokens = user.split(';')
if len(tokens) == 5:
passwd, id, last, first, email = tokens
properties = {
'username' : id,
'fullname' : '%s %s' % (first, last),
'email' : email.strip(),
}
try:
regtool.addMember(id, passwd, properties=properties)
print "Successfully added %s %s (%s) with email %s" % (first, last, id, email)
imported_count += 1
except ValueError, e:
print "Couldn't add %s: %s" % (id, e)
else:
print "Could not parse line %d because it had the following contents: '%s'" % (index, user)
index += 1
print "Imported %d users (from %d lines of CSV)" % (imported_count, index)
return printed
- Also at the site root, create a 'Script (Python') instance in the root folder of your plone instance with the id 'importPASfromCSV' and the script you just downloaded. Click the 'Add and Edit' button:
- In the following form click on the 'Test' tab:
This will execute the script and its output will be shown onscreen. - There is no step 6.
