接口名称
部门管理API(用户令牌)- IFeishuUserV1Departments
功能描述
部门是飞书组织架构里的一个基础实体,每个员工都归属于一个或多个部门。当前接口使用用户令牌访问,适应于用户应用场景。部门在飞书的身份标识包括department_id、open_department_id。
参考文档
https://open.feishu.cn/document/directory-v1/department/overview
函数列表
| 函数名称 | 功能描述 | 认证方式 | HTTP方法 |
|---|---|---|---|
| CreateDepartmentAsync | 创建新部门 | 用户令牌 | POST |
| UpdateDepartmentAsync | 更新部门信息 | 用户令牌 | PATCH |
| DeleteDepartmentByIdAsync | 删除指定部门 | 用户令牌 | DELETE |
| QueryDepartmentsAsync | 批量获取部门详细信息 | 用户令牌 | POST |
| QueryDepartmentsPageListAsync | 分页查询部门列表 | 用户令牌 | POST |
| SearchEmployeePageListAsync | 搜索部门信息 | 用户令牌 | POST |
函数详细内容
函数名称:创建部门
函数签名:
csharp
Task<FeishuApiResult<DepartmentCreateUpdateRequest>?> CreateDepartmentAsync(
[Body] DepartmentCreateUpdateRequest departmentCreateRequest,
[Query("employee_id_type")] string? employee_id_type = "open_id",
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:用户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| departmentCreateRequest | DepartmentCreateUpdateRequest | 是 | 创建部门的请求体 |
| employee_id_type | string | 否 | 用户ID类型,默认为"open_id" |
| department_id_type | string | 否 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success",
"data": {
"department": {
"department_id": "od-1234567890",
"name": {"zh_cn": "技术部"},
"parent_department_id": "0"
}
}
}说明:
- 用于在企业组织机构中创建新部门,支持设置部门名称、父部门、负责人等信息
- 父部门为根部门时,parent_department_id应设置为"0"
- custom_department_id不能以"od-"开头,需符合正则规则:^[a-zA-Z0-9][a-zA-Z0-9_-@.]{0,63}$
代码示例:
csharp
var request = new DepartmentCreateUpdateRequest
{
Department = new DepartmentInfo
{
Name = new I18nContents { ZhCn = "技术部" },
ParentDepartmentId = "0",
EnabledStatus = true,
OrderWeight = "100"
}
};
var result = await _feishuUserV1Departments.CreateDepartmentAsync(request);
if (result?.Code == 0)
{
Console.WriteLine("部门创建成功");
}函数名称:更新部门信息
函数签名:
csharp
Task<FeishuNullDataApiResult?> UpdateDepartmentAsync(
[Path] string department_id,
[Body] DepartmentCreateUpdateRequest departmentUpdateRequest,
[Query("employee_id_type")] string? employee_id_type = "open_id",
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:用户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| department_id | string | 是 | 部门ID |
| departmentUpdateRequest | DepartmentCreateUpdateRequest | 是 | 更新部门的请求体 |
| employee_id_type | string | 否 | 用户ID类型,默认为"open_id" |
| department_id_type | string | 否 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success"
}说明:
- 用于更新企业组织机构部门信息,仅更新显式传参的部分
- department_id需要与department_id_type的取值保持一致
代码示例:
csharp
var request = new DepartmentCreateUpdateRequest
{
Department = new DepartmentInfo
{
Name = new I18nContents { ZhCn = "研发技术部" },
OrderWeight = "200"
}
};
var result = await _feishuUserV1Departments.UpdateDepartmentAsync("od-1234567890", request);
if (result?.Code == 0)
{
Console.WriteLine("部门更新成功");
}函数名称:删除部门
函数签名:
csharp
Task<FeishuNullDataApiResult?> DeleteDepartmentByIdAsync(
[Path] string department_id,
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:用户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| department_id | string | 是 | 部门ID |
| department_id_type | string | 否 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success"
}说明:
- 从企业组织机构中删除指定的部门
- 删除部门前请确保该部门下没有子部门和员工
代码示例:
csharp
var result = await _feishuUserV1Departments.DeleteDepartmentByIdAsync("od-1234567890");
if (result?.Code == 0)
{
Console.WriteLine("部门删除成功");
}函数名称:批量获取部门信息
函数签名:
csharp
Task<FeishuApiResult<DepartmentListResult>?> QueryDepartmentsAsync(
[Body] DepartmentQueryRequest departmentQueryRequest,
[Query("employee_id_type")] string? employee_id_type = "open_id",
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:用户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| departmentQueryRequest | DepartmentQueryRequest | 是 | 部门查询参数请求体 |
| employee_id_type | string | 否 | 用户ID类型,默认为"open_id" |
| department_id_type | string | 否 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success",
"data": {
"departments": [
{
"department_id": "od-1234567890",
"name": {"zh_cn": "技术部"},
"parent_department_id": "0"
}
],
"abnormals": []
}
}说明:
- 支持传入多个部门ID,返回每个部门的详细信息
- required_fields参数控制返回的字段列表
代码示例:
csharp
var request = new DepartmentQueryRequest
{
DepartmentIds = new List<string> { "od-1234567890", "od-1234567891" },
RequiredFields = new List<string> { "name", "parent_department_id", "enabled_status" }
};
var result = await _feishuUserV1Departments.QueryDepartmentsAsync(request);
if (result?.Code == 0)
{
foreach (var dept in result.Data.Departments)
{
Console.WriteLine($"部门名称:{dept.Name}");
}
}函数名称:分页查询部门列表
函数签名:
csharp
Task<FeishuApiResult<DepartmentPageListResult>?> QueryDepartmentsPageListAsync(
[Body] FilterSearchRequest filterSearchRequest,
[Query("employee_id_type")] string? employee_id_type = "open_id",
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:用户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| filterSearchRequest | FilterSearchRequest | 是 | 字段过滤查询条件请求体 |
| employee_id_type | string | 否 | 用户ID类型,默认为"open_id" |
| department_id_type | string | 否 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success",
"data": {
"departments": [
{
"department_id": "od-1234567890",
"name": {"zh_cn": "技术部"}
}
],
"page_token": "next_page_token",
"has_more": true
}
}说明:
- 用于依据指定条件,批量获取符合条件的部门详情列表
- 支持分页查询和字段过滤
代码示例:
csharp
var request = new FilterSearchRequest
{
FilterConditions = new List<FilterCondition>
{
new FilterCondition { Field = "enabled_status", Operator = "eq", Value = true }
},
PageSize = 20,
PageToken = ""
};
var result = await _feishuUserV1Departments.QueryDepartmentsPageListAsync(request);
if (result?.Code == 0)
{
Console.WriteLine($"共找到 {result.Data.Departments.Count} 个部门");
}函数名称:搜索部门信息
函数签名:
csharp
Task<FeishuApiResult<DepartmentPageListResult>?> SearchEmployeePageListAsync(
[Body] PageSearchRequest pageSearchRequest,
[Query("employee_id_type")] string? employee_id_type = "open_id",
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:用户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| pageSearchRequest | PageSearchRequest | 是 | 分页查询参数请求体 |
| employee_id_type | string | 否 | 用户ID类型,默认为"open_id" |
| department_id_type | string | 否 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success",
"data": {
"departments": [
{
"department_id": "od-1234567890",
"name": {"zh_cn": "技术部"}
}
],
"page_token": "next_page_token",
"has_more": true
}
}说明:
- 用于搜索部门信息,通过部门名称等关键词搜索部门信息
- 返回符合条件的部门列表
代码示例:
csharp
var request = new PageSearchRequest
{
Query = "技术",
PageSize = 10,
PageToken = ""
};
var result = await _feishuUserV1Departments.SearchEmployeePageListAsync(request);
if (result?.Code == 0)
{
foreach (var dept in result.Data.Departments)
{
Console.WriteLine($"搜索结果:{dept.Name}");
}
}