Createdby and UpdatedBy in /users endpoint

Hello everyone

I’m trying to retrive createdBy and lastUpdatedBy of users via the endpoint /api/users , but these fields don’t appear in the result (nevertheless I have the created and lastUpdated fields).

Is there any way to retrieve these two fields with the API ?

Regards

Hi, @didate

To retrieve the createdBy and lastUpdatedBy fields for users via the /api/users endpoint, you need to explicitly request these fields using the fields parameter in your API call. By default, these fields may not be included in the standard response, which is why you only see created and lastUpdated.

Here is how you can modify your API call:

https://your-dhis2-instance/api/users?fields=id,name,created,createdBy,lastUpdated,lastUpdatedBy&paging=false

This request will return the id, name, created, createdBy, lastUpdated, and lastUpdatedBy fields for all users without pagination.

If you want the response in CSV format, you can modify the request like this:

https://your-dhis2-instance/api/users.csv?fields=id,name,created,createdBy,lastUpdated,lastUpdatedBy&paging=false

This will allow you to download the data in CSV format.

Let me know if you encounter any issues or need further assistance!

Hello @elmoujarrade

Thank you for your message.

I forgot to specify that I used the value “all” for the parameter fields as following

/api/users.json?fields=*

It is supposed to return all fields.

By the way I’ve tried to list fields as you suggest but I got a result without createdby and lastUpdatedBy

/api/users.json?fields=id,createdBy,lastUpdatedBy

Here is the result

image

NB: I’m using DHIS 2.40.

Regards

Hi, @didate

I wanted to follow up on our earlier discussion regarding the createdBy and lastUpdatedBy fields when retrieving user information via the /api/users endpoint.

I tested the API using DHIS2 version 2.38, and I was able to retrieve these fields without any issues. Here’s the request I used:

https://your-dhis2-instance/api/users?fields=id,name,created,createdBy,lastUpdated,lastUpdatedBy&paging=false

And here’s an anonymized example of the results I obtained:

<metadata xmlns="http://dhis2.org/schema/dxf/2.0">
  <users>
    <user id="AnonymizedID1">
      <lastUpdatedBy>
        <displayName>Anonymized Name 1</displayName>
        <name>Anonymized Name 1</name>
        <username>AnonymizedUsername1</username>
      </lastUpdatedBy>
      <createdBy>
        <displayName>Anonymized Name 1</displayName>
        <name>Anonymized Name 1</name>
        <username>AnonymizedUsername1</username>
      </createdBy>
    </user>
    <user id="AnonymizedID2">
      <lastUpdatedBy>
        <displayName>Anonymized Name 2</displayName>
        <name>Anonymized Name 2</name>
        <username>AnonymizedUsername2</username>
      </lastUpdatedBy>
      <createdBy>
        <displayName>Anonymized Name 2</displayName>
        <name>Anonymized Name 2</name>
        <username>AnonymizedUsername2</username>
      </createdBy>
    </user>
  </users>
</metadata>

Given that the createdBy and lastUpdatedBy fields are correctly returned in my case, it seems that the issue you’re facing may be related to DHIS2 version 2.40. There might have been changes in how these fields are handled or exposed via the API in this version.

I would recommend checking the API schema for your version or reaching out to the DHIS2 development team for further clarification on any changes between versions 2.38 and 2.40.

Best regards,

I wanted to follow up on our previous conversation about retrieving the createdBy and lastUpdatedBy fields using the /api/users endpoint. I decided to test the API on the DHIS2 Play instance (version 2.40.4.1), and I was able to retrieve these fields correctly.

Here’s the API call I used:

https://play.im.dhis2.org/stable-2-40-4-1/api/39/users?fields=id,createdBy,lastUpdatedBy&paging=false

JSON Response

When testing on the Play instance, I received the following JSON response structure, which includes the createdBy and lastUpdatedBy fields:

{
  "users": [
    {
      "id": "FLDWwCTIsv9",
      "createdBy": {
        "id": "GOLswS44mh8",
        "name": "Tom Wakiki",
        "displayName": "Tom Wakiki",
        "username": "system"
      },
      "lastUpdatedBy": {
        "id": "GOLswS44mh8",
        "name": "Tom Wakiki",
        "displayName": "Tom Wakiki",
        "username": "system"
      }
    },
    // ... other users
  ]
}

This JSON response correctly includes both createdBy and lastUpdatedBy fields, providing the user ID, name, displayName, and username for the respective fields.

XML Response

I also tested the response in XML format to check if it delivers the same data correctly. Here’s the API call for the XML format:

https://play.im.dhis2.org/stable-2-40-4-1/api/39/users.xml?fields=id,createdBy,lastUpdatedBy&paging=false

And here’s an example of the XML response:

<users>
  <user id="FLDWwCTIsv9">
    <createdBy>
      <id>GOLswS44mh8</id>
      <name>Tom Wakiki</name>
      <displayName>Tom Wakiki</displayName>
      <username>system</username>
    </createdBy>
    <lastUpdatedBy>
      <id>GOLswS44mh8</id>
      <name>Tom Wakiki</name>
      <displayName>Tom Wakiki</displayName>
      <username>system</username>
    </lastUpdatedBy>
  </user>
  <!-- ... other users -->
</users>

CSV Response

Additionally, if you need the data in CSV format, you can use this API call:

https://play.im.dhis2.org/stable-2-40-4-1/api/39/users.csv?fields=id,createdBy,lastUpdatedBy&paging=false

The CSV response will contain the relevant fields in a tabular format, making it easy to import into Excel or other data processing tools.

Conclusion

Based on my tests, the createdBy and lastUpdatedBy fields are indeed returned correctly in JSON, XML, and CSV formats on the Play instance. If you’re not seeing the same results, it could be related to a specific configuration or issue with the version of DHIS2 you’re using. I would recommend testing on the Play instance to see if the issue persists.

Let me know if you need further assistance or if you’d like more details!

Best regards,

@elmoujarrade Thank you for your messages.

Yes you are right, I disabled the pagination (paging=false) to get whole list of users and I noticed that some users have the fields createdBy and lastUpdatedBy but most of them do not have these fields.

Only the users I recently updated (after the migration to 2.40) have these fields.

NB: It’s not maybe related but this DHIS2 has been created with the DHIS 2.33 and migrated to 2.36 and now to 2.40.

Best Regards.

1 Like

Hello @elmoujarrade

I fixed the issue by exporting the users and re-importing them. So now I have the createdBy and lastUpdateBy fields.

@elmoujarrade thank you for your support.

Regards.

2 Likes