Synchronize User Data from DingTalk

DingTalkProfessional Edition+

Introduction

The DingTalk plugin synchronizes users and departments from a DingTalk organization to NocoBase. It supports full synchronization on demand and incremental updates through either an HTTP callback or a Stream connection.

Before you begin

  1. Install and enable the DingTalk and User Data Synchronization plugins.
  2. Create an internal application in the DingTalk developer console.
  3. Grant the contact permissions and configure the data permission scope described below.
  4. Copy the application's Client ID and Client Secret. See Authentication: DingTalk for the credential setup.

Configure contact permissions and the data permission scope

Open Permission Management for the application in the DingTalk developer console and grant the following contact permissions.

PermissionIdentifierRequiredPurpose
Read department informationqyapi_get_department_listYesRead the department list, names, and hierarchy.
Read department membersqyapi_get_department_memberYesRead the member list of each department.
Read member informationqyapi_get_memberYesRead member details and department memberships.
Employee mobile number informationfieldMobileWhen using mobile numbersSynchronize mobile numbers. This permission is required when User unique identifier field is mobile.
Email and other personal informationfieldEmailNoGrant this permission when user email addresses need to be synchronized.

After granting the permissions, configure the application's Data Permission Scope (also called Contact Permission Scope or Visibility Range in some console versions) to include the departments and employees that may be synchronized. Select all employees for a full organization synchronization. If only selected departments or employees are included, NocoBase synchronizes only those entries.

Warning

API permissions determine which fields the application can read, while the data permission scope determines which departments and employees it can read. Both must be configured. Event subscriptions do not replace contact read permissions: after receiving an event, NocoBase still calls DingTalk APIs to retrieve the latest user or department information.

If the same DingTalk application is also used for sign-in, grant the personal-information permissions described in Authentication: DingTalk. Those sign-in permissions are not required solely for user data synchronization.

Add a DingTalk synchronization source

Go to Users & Permissions > Synchronize, click Add new, and select DingTalk as the type.

Configure the following fields:

FieldDescription
Source nameA unique name for this synchronization source.
EnabledStarts event reception for this source and enables synchronization tasks.
Client IDThe Client ID of the DingTalk internal application. Environment variables and secrets are supported.
Client SecretThe Client Secret of the DingTalk internal application. Environment variables and secrets are supported.
User unique identifier fieldSelect mobile or unionId. Keep the selected field stable after the first synchronization. Users without a value for the selected field are skipped.
Event receiving modeSelect HTTP callback or Stream mode for incremental user and department changes.

Save and enable the source, then click Sync to complete the initial full synchronization before relying on incremental events.

Choose an event receiving mode

Stream mode

Stream mode establishes an outbound persistent connection from the NocoBase server to DingTalk. It does not require a public callback URL, Token, or EncodingAESKey.

  1. In the DingTalk developer console, open the application's event subscription settings and select Stream mode.
  2. Subscribe to the user and department change events required by the application.
  3. In NocoBase, select Stream mode, save the source, and enable it.

The Stream client starts when the source is enabled. Updating, disabling, or deleting the source refreshes or closes the corresponding connection.

Info

The NocoBase server must be able to establish outbound connections to DingTalk. A reverse proxy or public inbound callback endpoint is not required for Stream mode.

HTTP callback

HTTP callback mode receives DingTalk events through a NocoBase callback URL.

  1. Select HTTP callback in NocoBase.
  2. Enter the Token and EncodingAESKey configured for the DingTalk event subscription.
  3. Save the source and copy the generated Event callback URL.
  4. Configure that URL in the DingTalk developer console and subscribe to the required user and department events.

The callback URL must be reachable by DingTalk. In a production environment, expose it through HTTPS and make sure the reverse proxy forwards the request path unchanged.

Supported incremental events

Both event receiving modes handle the following DingTalk events:

EventResult in NocoBase
user_add_orgCreate or update the user.
user_modify_orgUpdate the user.
user_leave_orgDelete the synchronized user.
org_dept_createCreate or update the department.
org_dept_modifyUpdate the department and synchronize its users.
org_dept_removeDelete the synchronized department.

Synchronized fields

Department fields

DingTalk fieldNocoBase field or purpose
dept_idSource-unique department identifier.
nameDepartment name.
parent_idParent department used to build the department hierarchy. If the parent is outside the data permission scope, the department is synchronized as a root department.

User fields

DingTalk fieldNocoBase field or purpose
mobile or unionidGenerates the source-unique user identifier and username according to User unique identifier field. A user without the selected field is skipped.
nameUser nickname.
mobilePhone number. Requires the Employee mobile number information permission.
email, falling back to org_emailEmail address. Requires the Email and other personal information permission.
dept_id_listDepartment memberships. Only departments within the data permission scope are retained.
dept_order_listPrimary department.
leader_in_deptWhether the user is an owner of the corresponding department.

Department owners

DingTalk uses leader_in_dept in the user details to indicate whether the user is an owner of each department they belong to. NocoBase synchronizes this flag separately for each department: the same user can own multiple departments, and an owned department does not have to be the user's primary department. Only departments within the data permission scope are included.

When an owner flag is removed in DingTalk, the corresponding owner flag in NocoBase is removed by the next synchronization. Owner status changed manually in NocoBase may be overwritten by DingTalk data during the next synchronization.

Full and incremental synchronization use the same field mapping. Other DingTalk user fields, such as avatar, job title, and employee number, are not currently synchronized.

Troubleshooting

  • If synchronization returns no data or an entire department is missing, verify the three required contact read permissions and confirm that the department is included in the data permission scope.
  • If a user is present but their mobile number or email address is empty, verify the Employee mobile number information or Email and other personal information permission respectively.
  • If DingTalk reports that a department or employee is outside the permission scope, expand the application's data permission scope instead of only resubscribing to events.
  • If users are skipped, verify that they have a value for the configured unique identifier field.
  • For Stream mode, check the application logs for Dingtalk stream client starting, Dingtalk stream client started, or connection errors.
  • For HTTP callback mode, verify that the callback URL is publicly reachable and that Token and EncodingAESKey match the DingTalk configuration.
  • Run a manual full synchronization after changing the application's permissions or visibility range.