Skip to main content
Call this API to query all parent departments that a specific user belongs to. This API is not restricted by the Contacts permission scope.

Request

Basic information
HTTP URLhttps://oapi.dingtalk.io/topapi/v2/department/listparentbyuser
HTTP MethodPOST
Supported app typesappType-Internal appappType-Third-party enterprise app
Required permissionspermission-qyapi_get_department_list-Read permission for Contacts department information

Query parameters

NameTypeRequiredExampleDescription
access_tokenStringYesbE74xxxxThe App credential used to call this API. - For an Internal app, obtain it through the API for getting the access_token of an Internal app. - For a Third-party enterprise app, obtain it through the API for getting the access_token of a Third-party enterprise.

Request body

NameTypeRequiredExampleDescription
useridStringYes100The user ID of the user to query.

Request example

curl -X POST "https://oapi.dingtalk.io/topapi/v2/department/listparentbyuser" \
-H 'Content-Type:application/x-www-form-urlencoded;charset=utf-8' \
-d 'access_token=f3c2f4xxxxf65767' \
-d 'userid=100'
Java
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.io/topapi/v2/department/listparentbyuser");
OapiV2DepartmentListparentbyuserRequest req = new OapiV2DepartmentListparentbyuserRequest();
req.setUserid("100");
OapiV2DepartmentListparentbyuserResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
Python
import dingtalk.api

req=dingtalk.api.OapiV2DepartmentListparentbyuserRequest("https://oapi.dingtalk.io/topapi/v2/department/listparentbyuser")

req.userid="100"
try:
  resp= req.getResponse(access_token)
  print(resp)
except Exception,e:
  print(e)
PHP
include "TopSdk.php";
date_default_timezone_set('Asia/Shanghai');

$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_POST , DingTalkConstant::$FORMAT_JSON);
$req = new OapiV2DepartmentListparentbyuserRequest;
$req->setUserid("100");
$resp = $c->execute($req, $access_token, "https://oapi.dingtalk.io/topapi/v2/department/listparentbyuser");
C#
IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.io/topapi/v2/department/listparentbyuser");
OapiV2DepartmentListparentbyuserRequest req = new OapiV2DepartmentListparentbyuserRequest();
req.Userid = "100";
OapiV2DepartmentListparentbyuserResponse rsp = client.Execute(req, access_token);
Console.WriteLine(rsp.Body);

Response

Response body

NameTypeExampleDescription
errcodeNumber0The return code.
request_idStringnfe328zf67ecThe request ID.
errmsgStringInvalid useridThe error message returned when the call failed.
resultDeptListParentByUserResponseThe returned result.
parent_listDeptParentResponse[]The collection of parent department lists.
parent_dept_id_listNumber[][456, 123, 1]The parent department list.

Response body example

{
  "errcode": 0,
  "errmsg":"ok",
  "result": {
    "parent_list": [
      {
        "parent_dept_id_list": [
          37xxxx95,
          1
        ]
      }
    ]
  },
  "request_id": "nfe328zf67ec"
}

Error codes

If an error occurs when calling this API, find the solution in the Global error codes document based on the error message.
Error code (errcode)Error message (errmsg)Solution
43007Authorization requiredConfirm that the access_token has the required Permissions.
33012Invalid userIdConfirm that the userId is correct.
400002Invalid parameterConfirm that the parameter is entered as required.
-1System busyTry again later.