7

I would like to export users into a CSV in Drupal 7. I believe I can use the module Views Data Export, but I am not sure how to do that or if it is the correct module I would like.

2

4 Answers 4

13

Yes, Views data export is your best choice.

Create a view displaying users and add a page display. Add the fields you want in your export and change any additional settings you want.

Then add a display of type Data export, and set it to attach to the Page display. This way it'll add a link to the file download to that page (You can probably also skip the Page display and only add a Data export display with a path defined).

Choose your export format (CSV) under format settings. If you have a lot of users, make sure to choose batch operation under 'Data export settings'.

It's also a good idea to require a specific permission or role to access the view.

2
  • 1
    I see when using pager, unable to export content in current page. For example if you are on page 2, listing 10 recoreds in each page, csv link should export from 11th to 20th record there. Even using parent sort checked. Apr 27, 2015 at 10:44
  • When I use the Batch option, Role column is empty in CSV. any idea?
    – batMask
    Dec 18, 2019 at 10:03
3

Yes, the Views Data Export is the right module, but if you've big data files, you've to increase proper PHP limits, such as: PHP max_execution_time and memory_limit, unless you're using batch process, so you should be fine. You should increase the limits especially when using export via drush which is supported (note it's using separate PHP settings file).

To export users using custom function, check: How can I export all users in a CSV format?

which is basically the following custom code which you can use within some callback function:

drupal_add_http_header('Content-Type', 'text/csv');
drupal_add_http_header('Content-Disposition', 'attachment; filename=users.csv');
$results = db_query("SELECT * FROM {users}");
$csvdata = 'Uid,Username,Email' . PHP_EOL;
foreach ($results as $record) {
   $row = array();
   $row[] = $record->uid; 
   $row[] = $record->name; 
   $row[] = $record->mail; 
   $csvdata .= implode(',', $row) . PHP_EOL;
}
print $csvdata;
drupal_exit();
0

Another option to address these requirements is by using the Forena module, as detailed below.

Delivered reports

After you install/enable it, you get these out-of-the-box Drupal Administration reports that are delivered with it (among many other reports):

1) Active Users

+-----------+-----------------------+-------------------------+---------+
| User      | Email                 | Last Logged in          | Actions |
+-----------+-----------------------+-------------------------+---------+
| Webmaster | [email protected] | Sun, 03/27/2016 - 18:39 | logs    |
| Admin     | [email protected]      | Thu, 04/07/2016 - 12:47 | logs    |
| Pierre    | [email protected]    | Fri, 04/01/2016 - 18:19 | logs    |
+-----------+-----------------------+-------------------------+---------+

Using the hyperlink "logs" (in the column titled "Actions"), you get a new report (= Logs for user) showing the most recent log entries for the selected user with "type", "message", "timestamp" and with a subsequent hyperlink ("details") that will show additional details for the selected log record (such as "severity", "hostname", etc).

2) Role Details

After selecting any of the available roles (= a report parameter), you get a report that looks like in this example:

+-----------+---------+------------------------------+
| User Name | User Id | Actions                      |
+-----------+---------+------------------------------+
| Admin     | 1       | View Profile    Edit Profile |
| Webmaster | 8       | View Profile    Edit Profile |
+-----------+---------+------------------------------+

The hyperlinks View Profile / Edit Profile can be used to actually view/edit the profile of the selected user.

Tuning/cloning the delivered reports

Optionally, if you want to customize these reports, you can use its report writing features. Such as its WYSIWYG report writer and/or the Forena Query Builder (comes with Forena as a sub-module) to first create a query / report that fits your needs. For a video tutorial about this query writing tool, checkout Define data blocks with optional filters.

The result of it all could be a tabular output and/or graph. After the report content matches the specifications, you could save it as (e.g.) a CSV file. Other supported formats for saving a report are XLS, DOC, HTML, PDF, etc.

Video demos and tutorials

Video demos and tutorials contains quite some links to live demos about Forena.

0

To include custom fields from your users you can use:

drupal_add_http_header('Content-Type', 'text/csv');
drupal_add_http_header('Content-Disposition', 'attachment; filename=users.csv');
$csvdata = 'Uid,Username,Email,Firstname' . PHP_EOL;

$query = new EntityFieldQuery();

$query->entityCondition('entity_type', 'user');
$result = $query->execute();

$uids = array_keys($result['user']);

// THIS IS YOUR ARRAY OF UIDS.
$users = user_load_multiple($uids);

foreach ($users as $record) {
   $row = array();
   $row[] = $record->uid; 
   $row[] = $record->name; 
   $row[] = $record->mail; 

   // Custom fields, for example
   $row[] = $record->field_first_name_user['und'][0]['value'] ?? ''; 
   $csvdata .= implode(',', $row) . PHP_EOL;
}
print $csvdata;
drupal_exit();

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.