> ## Documentation Index
> Fetch the complete documentation index at: https://help.dingtalk.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Get all parent departments of a specific user

> Call this API to query the list of all parent departments a specified user belongs to. Not restricted by the Contacts permission scope.

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 URL              | [https://api.dingtalk.io/topapi/v2/department/listparentbyuser](https://api.dingtalk.io/topapi/v2/department/listparentbyuser) |
| HTTP Method           | POST                                                                                                                           |
| Supported app types   | appType-Internal appappType-Third-party enterprise app                                                                         |
| Required permissions  | permission-qyapi\_get\_department\_list-Read permission for Contacts department information                                    |

### Query parameters

| Name          | Type   | Required | Example  | Description                                                                                                                                                                                                                                                             |
| ------------- | ------ | -------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| access\_token | String | Yes      | bE74xxxx | The 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

| Name   | Type   | Required | Example | Description                       |
| ------ | ------ | -------- | ------- | --------------------------------- |
| userid | String | Yes      | 100     | The user ID of the user to query. |

### Request example

```curl theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl -X POST "https://api.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

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
DingTalkClient client = new DefaultDingTalkClient("https://api.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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import dingtalk.api

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

req.userid="100"
try:
  resp= req.getResponse(access_token)
  print(resp)
except Exception,e:
  print(e)
```

PHP

```php theme={"theme":{"light":"github-light","dark":"github-dark"}}
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://api.dingtalk.io/topapi/v2/department/listparentbyuser");
```

C#

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
IDingTalkClient client = new DefaultDingTalkClient("https://api.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

| Name                   | Type                         | Example        | Description                                      |
| ---------------------- | ---------------------------- | -------------- | ------------------------------------------------ |
| errcode                | Number                       | 0              | The return code.                                 |
| request\_id            | String                       | nfe328zf67ec   | The request ID.                                  |
| errmsg                 | String                       | Invalid userid | The error message returned when the call failed. |
| result                 | DeptListParentByUserResponse |                | The returned result.                             |
| parent\_list           | DeptParentResponse\[]        |                | The collection of parent department lists.       |
| parent\_dept\_id\_list | Number\[]                    | \[456, 123, 1] | The parent department list.                      |

### Response body example

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "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](/open/development/server-api-error-codes-1) document based on the error message.

| Error code (errcode) | Error message (errmsg) | Solution                                                     |
| -------------------- | ---------------------- | ------------------------------------------------------------ |
| 43007                | Authorization required | Confirm that the access\_token has the required Permissions. |
| 33012                | Invalid userId         | Confirm that the userId is correct.                          |
| 400002               | Invalid parameter      | Confirm that the parameter is entered as required.           |
| -1                   | System busy            | Try again later.                                             |
