You are here: Home / Documentation / How To's / Create many users with a PloneFormGen form

Create many users with a PloneFormGen form

by nguyen — published Dec 03, 2014 12:12 PM, last modified Aug 16, 2016 10:49 AM
and optionally assign them to a group

This is probably the most elegant way to mass-create local users in a Plone site, since it doesn't require that you use the ZMI nor add code on the server's file system.

  1. Create a PloneFormGen form (ie. add a form folder)
  2. Click on the QuickEdit tab.
  3. In the Toolbox on the right, click on "More Fields" and drag a "Lines field" into the form.
  4. Give the lines field the title "New Account Records" (the ID of field will automatically default to "new-account-records"... this is important!)
  5. In the Toolbox, click on "Actions" and drag a "Custom Script Adapter" into the "actions" area below the "field" area.
  6. Give the new custom script adapter any title you like, e.g. "Custom script to create users".
  7. For "Proxy role" select "Manager".
  8. In the Script Body, paste the script below.
  9. You can remove the other fields in the form.


Here is what the form will look like.



Here is what the form looks like in quickedit mode:


Testing the Form

To test the form, you could enter lines like this in the lines field:

Bob Dylan,,Reviewers
Petula Clark,,Administrators

Each new user will receive an email message containing a link that lets them set their password.


Script Code

Here is the script:

# Available parameters:
#  fields  = HTTP request form fields as key value pairs
#  request = The current HTTP request. 
#            Access fields by request.form["myfieldname"]
#  ploneformgen = PloneFormGen object
# Return value is not processed

lines = fields['new-account-records']
pr = context.portal_registration
pg = context.portal_groups
for line in lines:
  tokens = line.split(',')
  if len(tokens) == 3:
    fullname,email,groupId = tokens
    id = (email.split('@'))[0]
  elif len(tokens) == 4:
    fullname,email,groupId,id = tokens
    raise ValueError, 'Each line should contain: fullname,email,groupId[,id]. The offending line is: ''%s''' % line
  email = email.strip()
  properties = { 'username' : id, 'fullname' : fullname, 'email' : email }
  context.plone_log("create-user-accounts","Created username %s, full name %s, email %s" % (id, fullname, email))
  #pr.mailPassword(id, request)
  context.plone_log("create-user-accounts","Sent email notification to email %s" % (email))
  if groupId:
    group = pg.getGroupById(groupId)
    context.plone_log("create-user-accounts","Added username %s to group %s" % (id, groupId))

It is also available at