Batch-adding Users to Plone using CSV (Excel) files
This How-to applies to:
Plone 2.5.x
This How-to is intended for:
Site Administrators
- 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:

- Download this script.
- 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.
Exception
Line 25:
'emai': email,
to
'email': email.strip()
seems reasonable
expception thrown, but user added anyway?
Couldn't add userid: The login name you selected is already in use or is not valid. Please choose another.
my users.csv is
password;userid;Lastname;Firstname;email@somewhere.com
okay, not real data, but should be syntactically OK.
The weirdest part is that the user seems to be added fine. I am using plone 2.5.2
need... more... input...
invalid login name error
How about adding plone profile picture as well?
Some keywords.
Bulk registration of plone users.
Bulk membership addition of plone users.
Mass registration of plone users.
Mass membership addition of plone users.
Bulk and mass user registration of members to plone from csv file.
Using e-mail's as member id's.
Re: Using e-mail's as member id's.
Script does not work
Error message:
2007-06-26T15:21:10 ERROR Zope.SiteErrorLog http://192.168.1.89:8080/importPASfromCSV
Traceback (innermost last):
Module ZPublisher.Publish, line 115, in publish
Module ZPublisher.mapply, line 88, in mapply
Module ZPublisher.Publish, line 41, in call_object
Module Shared.DC.Scripts.Bindings, line 311, in __call__
Module Shared.DC.Scripts.Bindings, line 348, in _bindAndExec
Module Products.PythonScripts.PythonScript, line 326, in _exec
Module None, line 14, in importPASfromCSV
- <PythonScript at /importPASfromCSV>
- Line 14
Module Products.CMFCore.utils, line 82, in getToolByName
AttributeError: portal_registration
What did I wrong?
Thanks
It works :-)
So it works.
CSV also from OpenOffice.org Calc
I would like to add that not only Excel is able to save in CSV-Files. I created my test-csv-file with OpenOffice.org Calc, another OpenSource-Software (License: LGPL). In the saving-dialog I had only to ask the questions for the seperator (;) and whether there should be quotations around the strings.
Adding Location Field
I used the method and works great "as it is", but when i tryed to add also the location to the users doesnt work anymore.
So looks my version:
<scripttext>
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) == 6:
passwd, id, last, first, email, location = tokens
properties = {
'username' : id,
'fullname' : '%s %s' % (first, last),
'email' : email.strip(),
'location' : location,
}
try:
regtool.addMember(id, passwd, properties=properties)
print "Successfully added %s %s (%s) with email %s" % (first, last, id, email, location)
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
</scripttext>
What i did wrong?
Thanks
Addition to support group assignment
from Products.CMFCore.utils import getToolByName
users = context['users.csv'].data.split('\n')
regtool = getToolByName(context, 'portal_registration')
+ grouptool = getToolByName(context, 'portal_groups')
index = 1
imported_count = 0
+ grp = grouptool.getGroupById('<insert the name of your group here>')
for user in users:
tokens = user.split(';')
***************
*** 28,33 ****
--- 30,37 ----
regtool.addMember(id, passwd, properties=properties)
print "Successfully added %s %s (%s) with email %s" % (first, last, id, email)
imported_count += 1
+ grp.addMember(id)
+ print "Successfully added %s %s to the group %s" % (first, last, 'ShipmentConfirmers')
except ValueError, e:
print "Couldn't add %s: %s" % (id, e)
else:
As you can see the group is hard-coded - it would be trivial to extend that to a parameter based approach (additional fields on each line indicating the required groups?), but this met my needs.
Addition to support group assignment
from Products.CMFCore.utils import getToolByName
users = context['users.csv'].data.split('\n')
regtool = getToolByName(context, 'portal_registration')
+ grouptool = getToolByName(context, 'portal_groups')
index = 1
imported_count = 0
+ grp = grouptool.getGroupById('<insert the name of your group here>')
for user in users:
tokens = user.split(';')
***************
*** 28,33 ****
--- 30,37 ----
regtool.addMember(id, passwd, properties=properties)
print "Successfully added %s %s (%s) with email %s" % (first, last, id, email)
imported_count += 1
+ grp.addMember(id)
+ print "Successfully added %s %s to the group %s" % (first, last, 'ShipmentConfirmers')
except ValueError, e:
print "Couldn't add %s: %s" % (id, e)
else:
As you can see the group is hard-coded - it would be trivial to extend that to a parameter based approach (additional fields on each line indicating the required groups?), but this met my needs.
Exception
Line 25:
'emai': email,
to
'email': email.strip()