部门接口
首先我们要创建一个表,用于存储部门信息,表名为Departments,结构如下:
获取部门列表
获取所有部门信息的代码参考:
Dim
ur As
String =
"https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={0}&id={1}"
Dim
hc As
New HttpClient(CExp(ur,Functions.Execute("GetQYAccessToken"),1))
Dim
jo As
JObject = JObject.Parse(hc.GetData())
If
jo("errcode")
= "0" Then
DataTables("Departments").DataRows.Clear()
For Each
jt As
JToken In
jo("department")
Dim dr
As DataRow=
DataTables("Departments").AddNew()
dr("id")
= jt("id")
dr("name")
= jt("name")
dr("parentid")
= jt("parentid")
dr("order")
= jt("order")
dr.Save
Next
Else
MessageBox.Show(jo.Tostring)
End
If
创建部门
创建部门的时候,部门ID可以由接口自动生成,例如:
Dim
ul As
String =
"https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={0}"
Dim
hc As
New HttpClient(CExp(ul,Functions.Execute("GetQYAccessToken")))
Dim
jo As
New
JObject
jo("name")
= "辉迅软件技术部"
jo("parentid")
= 1
'指定父部门id,1表示根部门
jo("order")
= 20
hc.Content
= jo.ToString()
jo =
JObject.Parse(hc.GetData)
If
jo("errcode")
= "0" Then
Dim dr
As DataRow =
DataTables("Departments").AddNew()
dr("name")
= "辉迅软件技术部"
dr("parentid")
= 1
dr("order")
= 20
dr("id")
= jo("id")
'获取自动生成的部门id
Else
MessageBox.Show(jo.ToString)
End
If
修改部门
例如要将ID为部门11的部门名称改为"辉迅软件市场部",可以参考代码:
Dim
ul As
String =
"https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={0}"
Dim
hc As
New HttpClient(CExp(ul,Functions.Execute("GetQYAccessToken")))
Dim
jo As
New
JObject
jo("name")
= "辉迅软件市场部"
'指定新的部门名称
jo("parentid")
= 1
jo("order")
= 20
jo("id")
= 11
hc.Content
= jo.ToString()
jo =
JObject.Parse(hc.GetData)
If
jo("errcode")
= "0" Then
Dim dr
As DataRow =
DataTables("Departments").Find("id
= 11")
dr("name")
= "辉迅软件市场部"
dr.Save()
Else
MessageBox.Show(jo.ToString)
End
If
提示:除了部门ID,其他属性都能修改。
删除部门
例如要删除ID为11的部门,可以参考代码:
Dim
ur As
String =
"https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={0}&id=11"
Dim
hc As
New HttpClient(CExp(ur,Functions.Execute("GetQYAccessToken")))
Dim
jo As
JObject = JObject.Parse(hc.GetData())
If
jo("errcode")
= "0" Then
DataTables("Departments").DeleteFor("id
= 11")
DataTables("Departments").Save
Else
MessageBox.Show(jo.Tostring)
End
If
提示:不能删除有子部门或成员的部门。