我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面的微软最有价值专家(Microsoft MVP),欢迎关注我的微信公众号 MSFTDynamics365erLuoYong ,回复394或者20200221可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!
在阅读本文之前建议先阅读我的文章 可以设置实体在Dynamics 365高级查找中不显示吗? 和 Dynamics 365 V9版本新的客户端API Xrm.WebApi.online.execute 使用实例 。
如下官方文档可以参考
- Create and update entity definitions using the Web API
- Retrieve metadata by name or MetadataId
- PublishXml Action
- Publish customizations
我这个需求是录入实体的逻辑名称,将改实体的所有自定义字段的审核属性(Auditing)设置为禁用(Disable)。
做这个操作需要拿到实体和字段的MetadataId,然后发起请求逐个字段来更改其属性,最后通过使用Xrm.WebApi.online.execute调用名称为PublishXml 的非绑定Action来发布实体。
不多说了,上代码:
var entiyLogicalName = "ly_demoentity"; var entityMetadataId = ""; var responseJSON = {}; var attributesToChange = []; var clientURL = Xrm.Utility.getGlobalContext().getClientUrl(); var req = new XMLHttpRequest(); req.open("GET", encodeURI(clientURL + "/api/data/v9.1/EntityDefinitions?$select=MetadataId&$filter=LogicalName eq '" + entiyLogicalName + "'"), 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 == 200) { responseJSON = JSON.parse(this.responseText); entityMetadataId = responseJSON.value[0].MetadataId; } } } req.send(); console.log(entiyLogicalName + ".MetadataId=" + entityMetadataId); //查询字段的时候要提出掉查找字段生成的附件字段(也就是查找字段名加上name) req = new XMLHttpRequest(); req.open("GET", encodeURI(clientURL + "/api/data/v9.1/EntityDefinitions(LogicalName='" + entiyLogicalName + "')/Attributes/?$filter=IsCustomAttribute eq true and IsAuditEnabled/Value eq true and IsValidForUpdate eq true&$select=MetadataId,LogicalName"), 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 == 200) { responseJSON = JSON.parse(this.responseText); responseJSON.value.forEach(function (item, index) { attributesToChange.push({ MetadataId: item.MetadataId, AttributeName: item.LogicalName }); }); } } } req.send(); console.log(entiyLogicalName + "实体共有" + attributesToChange.length + "个字段需要处理!"); attributesToChange.forEach(function (item,index) { req = new XMLHttpRequest(); req.open("PUT", encodeURI(clientURL + "/api/data/v9.1/EntityDefinitions(" + entityMetadataId + ")/Attributes(" + item.MetadataId + ")"), 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 /* complete */) { req.onreadystatechange = null; if (this.status == 204) { console.log("第" + (index + 1) + "个字段" + item.AttributeName + "修改成功!"); } else { var error = JSON.parse(this.response).error; Xrm.Utility.alertDialog("修改出错." + error.message); } } }; var sendData = { IsAuditEnabled: { Value: false } }; req.send(JSON.stringify(sendData)); }); console.log(entiyLogicalName + "实体的字段设置Auditing为false完毕,下一步发布本实体!"); var PublishXmlRequest = function (parameterXml) { this.ParameterXml = parameterXml; }; PublishXmlRequest.prototype.getMetadata = function () { return { boundParameter: null, parameterTypes: { "ParameterXml": { typeName: "Edm.String", structuralProperty: 1 } }, operationType: 0, operationName: "PublishXml", }; }; var parameterXml = "<publish><entities><entity>" + entiyLogicalName + "</entity></entities></publish>"; var publishXmlRequest = new PublishXmlRequest(parameterXml); Xrm.WebApi.online.execute(publishXmlRequest).then( function (result) { if (result.ok) { console.log("发布实体操作完成! Status: %s %s", result.status, result.statusText); } }, function (error) { console.log(error.message); } );
在使用之前记得修改第一行代码的实体逻辑名称为你要处理的实体,然后打开一个实体的窗体,按F12,在出来的 Console 窗口值执行即可,如果执行中断了,可以再次执行。
如果是查询某个实体的必须输入字段清单,可以利用类似如下的查询:
https://luoyongdemo.crm.dynamics.com/api/data/v9.1/EntityDefinitions(LogicalName='ly_test')/Attributes/?$filter=RequiredLevel/Value eq 'ApplicationRequired'&$select=LogicalName