Hello everyone, I was creating a dataset and I forget to share the first data element and cloned hundreds of data elements. Finally, I couldn’t see it on Mobile capture app because it was not shared. Can anyone help me in bulk share data elements that start with some prefixes to a user group, either with web API or other means. Please?
@tsegshsmart Yes, there’s a workaround for bulk sharing data elements in DHIS2.
The DHIS2 API provides a bulk sharing endpoint that allows you to apply sharing settings to multiple metadata objects at once. This is exactly what you need for your situation with the data elements.
You can use the following API endpoint to bulk share data elements(note: cannot be accessed over a GET method, use PATCH):
/api/dataElements/sharing
This endpoint is documented in the DHIS2 API documentation here.
To use this endpoint, you’ll need to send a PATCH request with a JSON payload that specifies the sharing settings and the data elements you want to update. Here’s an example of how you might structure your request:
- Create a JSON file (let’s call it
payload.json
) with the following structure:
{
"patch": {
"operations": [
{
"op": "add",
"path": "/userGroupAccesses/-",
"value": {
"id": "USER_GROUP_ID",
"access": "rw------"
}
}
]
},
"targetIds": {
"dataElements": ["ID1", "ID2", "ID3"]
}
}
Replace USER_GROUP_ID
with the ID of the user group you want to share with, and replace ID1
, ID2
, ID3
with the IDs of the data elements you want to share. If you’re using Postman, you’ll be able to attach the full list of data elements as a csv file in a runner. You can include as many data element IDs as needed.
- You can also use curl or another HTTP client to send the PATCH request:
curl -X PATCH -d @payload.json -H “Content-Type: application/json-patch+json” “https://your-dhis2-instance/api/dataElements/sharing”
Make sure to replace https://your-dhis2-instance
with the actual URL of your DHIS2 instance.
@Quoda Thanks for your response. I tried with postman and python environment.
I managed to filter the data elements that i want and directly paste it in the body, used PATCH with the correct url and using the api/dataElements/sharing. But finally I get the error showed below. I really appreciate a fix for it. Thanks again.
{
“httpStatus”: “Internal Server Error”,
“httpStatusCode”: 500,
“status”: “ERROR”,
“message”: “Cannot deserialize value of type org.hisp.dhis.jsonpatch.BulkJsonPatch
from Array value (token JsonToken.START_ARRAY
)\n at [Source: (org.apache.catalina.connector.CoyoteInputStream); line: 1, column: 1]”
}
hmm, seems there’s an issue with the format of the JSON payload. The server is expecting a specific JSON structure and not an array; that’s what the error says. I wish I could see your payload structure.
The structure looks like this
{
“id”: “ufQca433cKg”,
“sharing”: {
“userGroups”: {
“fiAW3MrnkLe”: {
“access”: “rw------”,
“id”: “fiAW3MrnkLe”
}
}
}
},
{
“id”: “JbMvB768ajI”,
“sharing”: {
“userGroups”: {
“fiAW3MrnkLe”: {
“access”: “rw------”,
“id”: “fiAW3MrnkLe”
}
}
}
},
Hi @tsegshsmart
Depending on your DHIS2 version, you may also have a look at those 2 Web applications if your want to avoid using API:
Sharing Settings by EyeSeeTea team
Or
Maintenance (Preview) by DHIS2 (The new version of Maintenance Web app by DHIS2 Core Team)