3、 获取人员id
获得部门列表后,我们就可以遍历部门列表,根据每一个部门的id获得该部门的所有人员信息。
访问地址:https://oapi.dingtalk.com/user/simplelist
参数:访问凭证、部门id。
调用接口后,获得所有的人员信息,将人员ID和人员姓名存放在数据字典中。
private Dictionary<string,string> GetDepartmentUsers(string Token,Dictionary<string,string> Depts, out string Message)
{
Message = null;
Dictionary<string,string> dic = new Dictionary<string, string>();
try
{
IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/simplelist");
OapiUserSimplelistRequest req = new OapiUserSimplelistRequest();
foreach(KeyValuePair<string,string> kp in Depts)
{
req.DepartmentId = Convert.ToInt64(kp.Key);
req.SetHttpMethod("GET");
OapiUserSimplelistResponse rsp = client.Execute(req, Token);
JObject jo = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(rsp.Body);
string errorCode = jo["errcode"].ToString();
if (errorCode == "0")
{
JArray jaUsers = (JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(jo["userlist"].ToString());
foreach (JObject joUser in jaUsers)
{
if (!dic.ContainsKey(joUser["userid"].ToString()))
dic.Add(joUser["userid"].ToString(), joUser["name"].ToString());
}
}
else
{
Message = jo["errmsg"].ToString();
break;
}
}
}
catch (Exception ex)
{
Message = ex.Message;
}
return dic;
}