知识就是力量,时间就是生命。这篇文章主要讲述常见的通过Web API执行associate和disassociate消息示例相关的知识,希望能为你提供帮助。
我是微软Dynamics 365 &
Power Platform方面的工程师/顾问罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面的微软最有价值专家(Microsoft MVP),欢迎关注我的微信公众号 MSFTDynamics365erLuoYong ,回复435或者20210220可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!
我以前的博文 Dynamics 365中的常用Associate和Disassociate消息汇总 介绍了通过组织服务执行Associate和disassociate消息的示例,博文 Dynamics 365 We API ODATA语法根据父记录查询子记录,根据子记录查询父记录(附上根据团队,队列名称查成员) 还介绍了对Associated数据进行查询的方法,以前还通过博文 Dynamics 365中使用Web API将查找字段的值设置为空值的方法。 介绍了取消关联的方法。官方文档请参考 Associate and disassociate entities using the Web API。今天我列出来一些常见关联和取消关联操作的写法。
1. 一个用户加入指定的一个团队。用户一次加入多个团队我没有找到一个请求的方法,当然使用我这博文 使用JS通过Web API执行批量操作,多个操作是一个事务! 提到的方法能提升效率。
var clientUrl = Xrm.Utility.getGlobalContext().getClientUrl();
var req = new XMLHttpRequest();
req.open("POST", clientUrl + "/api/data/v9.2/teams(7027A1F0-D247-EA11-A812-000D3A378A3A)/teammembership_association/$ref", false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json;
charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 204) {
var alertStrings = { confirmButtonLabel: "Yes", text: "操作成功!", title: "提示" };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
}
else {
var error = JSON.parse(this.response).error;
Xrm.Navigation.openErrorDialog({ message: "操作失败!" + error.message })
}
}
};
var reqContent = { "@odata.id": clientUrl + "/api/data/v9.2/systemusers(3a1eb63b-f546-ea11-a813-000d3a8c9f2b)" };
req.send(JSON.stringify(reqContent));
2. 一个用户一次添加一个角色。
var clientUrl = Xrm.Utility.getGlobalContext().getClientUrl();
var req = new XMLHttpRequest();
req.open("POST", clientUrl + "/api/data/v9.2/roles(E57BFF35-58B4-4725-AEE2-40FB18F4C8BD)/systemuserroles_association/$ref", false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json;
charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 204) {
var alertStrings = { confirmButtonLabel: "Yes", text: "操作成功!", title: "提示" };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
}
else {
var error = JSON.parse(this.response).error;
Xrm.Navigation.openErrorDialog({ message: "操作失败!" + error.message })
}
}
};
var reqContent = { "@odata.id": clientUrl + "/api/data/v9.2/systemusers(3a1eb63b-f546-ea11-a813-000d3a8c9f2b)" };
req.send(JSON.stringify(reqContent));
3.一个团队一次添加一个角色
var clientUrl = Xrm.Utility.getGlobalContext().getClientUrl();
var req = new XMLHttpRequest();
req.open("POST", clientUrl + "/api/data/v9.2/roles(E57BFF35-58B4-4725-AEE2-40FB18F4C8BD)/teamroles_association/$ref", false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json;
charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 204) {
var alertStrings = { confirmButtonLabel: "Yes", text: "操作成功!", title: "提示" };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
}
else {
var error = JSON.parse(this.response).error;
Xrm.Navigation.openErrorDialog({ message: "操作失败!" + error.message })
}
}
};
var reqContent = { "@odata.id": clientUrl + "/api/data/v9.2/teams(7027A1F0-D247-EA11-A812-000D3A378A3A)" };
req.send(JSON.stringify(reqContent));
4.一个用户一次加入一个queue,暂时发现用这个方法虽然不报错,但是也没有效果
var clientUrl = Xrm.Utility.getGlobalContext().getClientUrl();
var req = new XMLHttpRequest();
req.open("POST", clientUrl + "/api/data/v9.2/queues(FC9FD7F5-2073-EB11-A812-000D3A194E4E)/queue_system_user/$ref", false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json;
charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 204) {
var alertStrings = { confirmButtonLabel: "Yes", text: "操作成功!", title: "提示" };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
}
else {
var error = JSON.parse(this.response).error;
Xrm.Navigation.openErrorDialog({ message: "操作失败!" + error.message })
}
}
};
var reqContent = { "@odata.id": clientUrl + "/api/data/v9.2/systemusers(b875c79e-930f-ea11-a811-000d3a8c92c3)" };
req.send(JSON.stringify(reqContent));
可以用AddPrincipalToQueue Action来做,示例代码如下:
var clientUrl = Xrm.Utility.getGlobalContext().getClientUrl();
var req = new XMLHttpRequest();
req.open("POST", clientUrl + "/api/data/v9.2/queues(FC9FD7F5-2073-EB11-A812-000D3A194E4E)/Microsoft.Dynamics.CRM.AddPrincipalToQueue", false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json;
charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 204) {
var alertStrings = { confirmButtonLabel: "Yes", text: "操作成功!", title: "提示" };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
}
else {
var error = JSON.parse(this.response).error;
Xrm.Navigation.openErrorDialog({ message: "操作失败!" + error.message })
}
}
};
var reqContent = {
"Principal":"/systemusers(b875c79e-930f-ea11-a811-000d3a8c92c3)"
};
req.send(JSON.stringify(reqContent));
5,将一个用户从一个团队中移除
var clientUrl = Xrm.Utility.getGlobalContext().getClientUrl();
var req = new XMLHttpRequest();
req.open("DELETE", clientUrl + "/api/data/v9.2/teams(7027A1F0-D247-EA11-A812-000D3A378A3A)/teammembership_association(3a1eb63b-f546-ea11-a813-000d3a8c9f2b)/$ref", false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json;
charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 204) {
var alertStrings = { confirmButtonLabel: "Yes", text: "操作成功!", title: "提示" };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
}
else {
var error = JSON.parse(this.response).error;
Xrm.Navigation.openErrorDialog({ message: "操作失败!" + error.message })
}
}
};
req.send();
6,移除一个用户的一个角色
var clientUrl = Xrm.Utility.getGlobalContext().getClientUrl();
var req = new XMLHttpRequest();
req.open("DELETE", clientUrl + "/api/data/v9.2/roles(E57BFF35-58B4-4725-AEE2-40FB18F4C8BD)/systemuserroles_association(3a1eb63b-f546-ea11-a813-000d3a8c9f2b)/$ref", false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json;
charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 204) {
var alertStrings = { confirmButtonLabel: "Yes", text: "操作成功!", title: "提示" };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
}
else {
var error = JSON.parse(this.response).error;
Xrm.Navigation.openErrorDialog({ message: "操作失败!" + error.message })
}
}
};
req.send();
7,移除一个团队的一个角色
var clientUrl = Xrm.Utility.getGlobalContext().getClientUrl();
var req = new XMLHttpRequest();
req.open("DELETE", clientUrl + "/api/data/v9.2/roles(E57BFF35-58B4-4725-AEE2-40FB18F4C8BD)/teamroles_association(7027A1F0-D247-EA11-A812-000D3A378A3A)/$ref", false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json;
charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 204) {
var alertStrings = { confirmButtonLabel: "Yes", text: "操作成功!", title: "提示" };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
}
else {
var error = JSON.parse(this.response).error;
Xrm.Navigation.openErrorDialog({ message: "操作失败!" + error.message })
}
}
};
req.send();
8.移除一个queue中的一个成员:
var clientUrl = Xrm.Utility.getGlobalContext().getClientUrl();
var req = new XMLHttpRequest();
req.open("DELETE", clientUrl + "/api/data/v9.2/queues(FC9FD7F5-2073-EB11-A812-000D3A194E4E)/queue_system_user(b875c79e-930f-ea11-a811-000d3a8c92c3)/$ref", false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json;
charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 204) {
var alertStrings = { confirmButtonLabel: "Yes", text: "操作成功!", title: "提示" };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
}
else {
var error = JSON.parse(this.response).error;
Xrm.Navigation.openErrorDialog({ message: "操作失败!" + error.message })
}
}
};
req.send();
9. 一次添加多个成员到团队,这里利用 AddMembersTeam Action 来做。
var clientUrl = Xrm.Utility.getGlobalContext().getClientUrl();
var req = new XMLHttpRequest();
req.open("POST", clientUrl + "/api/data/v9.2/teams(7027A1F0-D247-EA11-A812-000D3A378A3A)/Microsoft.Dynamics.CRM.AddMembersTeam", false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json;
charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 204) {
var alertStrings = { confirmButtonLabel: "Yes", text: "操作成功!", title: "提示" };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
}
else {
var error = JSON.parse(this.response).error;
Xrm.Navigation.openErrorDialog({ message: "操作失败!" + error.message })
}
}
};
var reqContent = {
Members: [
{
"systemuserid":"3a1eb63b-f546-ea11-a813-000d3a8c9f2b"
},
{
"systemuserid": "b875c79e-930f-ea11-a811-000d3a8c92c3"
}
]
};
req.send(JSON.stringify(reqContent));
【常见的通过Web API执行associate和disassociate消息示例】
推荐阅读
- Power Apps component framework (PCF) 手把手入门实例
- 用户离职后如何删除其个人视图(Personal View)()
- Power Apps原生支持富文本展示和编辑文本字段了
- Dynamics 365的新特性Relevance Search介绍
- 获取组织服务报错:Ws-Tust authentication which has been dep
- Dynamics 365执行Remove Active Customizations的多种方法
- 在Cloud Flow和Workflow中使用多选选项集类型字段
- Dynamics 365中Customer类型字段的使用
- Dynamics 365中表的File数据类型列介绍