Microsoft Dynamics Online:将所有选项集项目复制到多行文本字段(使用 JavaScript)
Microsoft Dynamics Online: Copy all Option Set Items to Multiline Text field (using JavaScript)
我有一个包含 2 个自定义字段的表单:"选项集"和"多行文本"。我将JavaScript文件作为WebResource上传,并将该函数添加到Form的Onload事件中。我想知道如何使用 JavaScript 将所有选项集项目复制到多行文本框。
尝试使用此代码片段:此请求获取选项集的元数据,并以XML格式从CRM返回所有数据。
if (typeof (SDK) == "undefined") { SDK = { __namespace: true }; }
//This will establish a more unique namespace for functions in this library. This will reduce the
// potential for functions to be overwritten due to a duplicate name when the library is loaded.
SDK.OptionSet = {
_getServerUrl: function () {
///<summary>
/// Returns the URL for the SOAP endpoint using the context information available in the form
/// or HTML Web resource.
///</summary>
var ServicePath = "/XRMServices/2011/Organization.svc/web";
var serverUrl = "";
if (typeof GetGlobalContext == "function") {
var context = GetGlobalContext();
serverUrl = context.getServerUrl();
}
else {
if (typeof Xrm.Page.context == "object") {
serverUrl = Xrm.Page.context.getServerUrl();
}
else
{ throw new Error("Unable to access the server URL"); }
}
if (serverUrl.match(/'/$/)) {
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
}
return serverUrl + ServicePath;
},
GetOptionSetValuesRequest: function () {
var requestMain = ""
requestMain += "<s:Envelope xmlns:s='"http://schemas.xmlsoap.org/soap/envelope/'">";
requestMain += " <s:Body>";
requestMain += " <Execute xmlns='"http://schemas.microsoft.com/xrm/2011/Contracts/Services'" xmlns:i='"http://www.w3.org/2001/XMLSchema-instance'">";
requestMain += " <request i:type='"a:RetrieveAttributeRequest'" xmlns:a='"http://schemas.microsoft.com/xrm/2011/Contracts'">";
requestMain += " <a:Parameters xmlns:b='"http://schemas.datacontract.org/2004/07/System.Collections.Generic'">";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <b:key>MetadataId</b:key>";
requestMain += " <b:value i:type='"c:guid'" xmlns:c='"http://schemas.microsoft.com/2003/10/Serialization/'">00000000-0000-0000-0000-000000000000</b:value>";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <b:key>RetrieveAsIfPublished</b:key>";
requestMain += " <b:value i:type='"c:boolean'" xmlns:c='"http://www.w3.org/2001/XMLSchema'">true</b:value>";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <b:key>EntityLogicalName</b:key>";
requestMain += " <b:value i:type='"c:string'" xmlns:c='"http://www.w3.org/2001/XMLSchema'">"+ENTITYLOGICALNAME+"</b:value>";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <b:key>LogicalName</b:key>";
requestMain += " <b:value i:type='"c:string'" xmlns:c='"http://www.w3.org/2001/XMLSchema'">"+ATTRIBUTENAME+"</b:value>";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " </a:Parameters>";
requestMain += " <a:RequestId i:nil='"true'" />";
requestMain += " <a:RequestName>RetrieveAttribute</a:RequestName>";
requestMain += " </request>";
requestMain += " </Execute>";
requestMain += " </s:Body>";
requestMain += "</s:Envelope>";
var req = new XMLHttpRequest();
req.open("POST", SDK.OptionSet._getServerUrl(), false)
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
req.send(requestMain);
//work with the response here
var strResponse = req.responseXML.xml;
//alert(strResponse.toString());
}, __namespace: true};
响应将在 strResponse 中。您将需要解析它。这是相当大的回应,所以我只提供其中的一部分:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<ExecuteResponse xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ExecuteResult i:type="a:RetrieveAttributeResponse" xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
<a:ResponseName>RetrieveAttribute</a:ResponseName>
<a:Results xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
<a:KeyValuePairOfstringanyType>
<b:key>AttributeMetadata</b:key>
<b:value i:type="c:PicklistAttributeMetadata" xmlns:c="http://schemas.microsoft.com/xrm/2011/Metadata">
<!-- a lots of data -->
<c:OptionSet>
<c:MetadataId>761cd65c-1d0f-e311-bb04-d48564519ccb</c:MetadataId>
<c:HasChanged i:nil="true" />
<c:Description>
<a:LocalizedLabels />
<a:UserLocalizedLabel i:nil="true" />
</c:Description>
<c:DisplayName>
<a:LocalizedLabels>
<a:LocalizedLabel>
<c:MetadataId i:nil="true" />
<c:HasChanged i:nil="true" />
<a:IsManaged>false</a:IsManaged>
<a:Label>Previous Supplier</a:Label>
<a:LanguageCode>1033</a:LanguageCode>
</a:LocalizedLabel>
</a:LocalizedLabels>
<a:UserLocalizedLabel>
<c:MetadataId i:nil="true" />
<c:HasChanged i:nil="true" />
<a:IsManaged>false</a:IsManaged>
<a:Label>Previous Supplier</a:Label>
<a:LanguageCode>1033</a:LanguageCode>
</a:UserLocalizedLabel>
</c:DisplayName>
<c:IsCustomOptionSet>true</c:IsCustomOptionSet>
<c:IsCustomizable>
<a:CanBeChanged>true</a:CanBeChanged>
<a:ManagedPropertyLogicalName>iscustomizable</a:ManagedPropertyLogicalName>
<a:Value>true</a:Value>
</c:IsCustomizable>
<c:IsGlobal>false</c:IsGlobal>
<c:IsManaged>false</c:IsManaged>
<c:Name>ut_budgetconrtroller_ut_previoussupplier</c:Name>
<c:OptionSetType>Picklist</c:OptionSetType>
<c:Options>
<c:OptionMetadata>
<c:MetadataId i:nil="true" />
<c:HasChanged i:nil="true" />
<c:Description>
<a:LocalizedLabels />
<a:UserLocalizedLabel i:nil="true" />
</c:Description>
<c:IsManaged>false</c:IsManaged>
<c:Label>
<a:LocalizedLabels>
<a:LocalizedLabel>
<c:MetadataId i:nil="true" />
<c:HasChanged i:nil="true" />
<a:IsManaged>false</a:IsManaged>
<a:Label>Option 0</a:Label>
<a:LanguageCode>1033</a:LanguageCode>
</a:LocalizedLabel>
</a:LocalizedLabels>
<a:UserLocalizedLabel>
<c:MetadataId i:nil="true" />
<c:HasChanged i:nil="true" />
<a:IsManaged>false</a:IsManaged>
<a:Label>Option 0</a:Label>
<a:LanguageCode>1033</a:LanguageCode>
</a:UserLocalizedLabel>
</c:Label>
<c:Value>206350001</c:Value>
</c:OptionMetadata>
<c:OptionMetadata>
<c:MetadataId i:nil="true" />
<c:HasChanged i:nil="true" />
<c:Description>
<a:LocalizedLabels />
<a:UserLocalizedLabel i:nil="true" />
</c:Description>
<c:IsManaged>false</c:IsManaged>
<c:Label>
<a:LocalizedLabels>
<a:LocalizedLabel>
<c:MetadataId i:nil="true" />
<c:HasChanged i:nil="true" />
<a:IsManaged>false</a:IsManaged>
<a:Label>Option 1</a:Label>
<a:LanguageCode>1033</a:LanguageCode>
</a:LocalizedLabel>
</a:LocalizedLabels>
<a:UserLocalizedLabel>
<c:MetadataId i:nil="true" />
<c:HasChanged i:nil="true" />
<a:IsManaged>false</a:IsManaged>
<a:Label>Option 1</a:Label>
<a:LanguageCode>1033</a:LanguageCode>
</a:UserLocalizedLabel>
</c:Label>
<c:Value>206350002</c:Value>
</c:OptionMetadata>
</c:Options>
</c:OptionSet>
</b:value>
</a:KeyValuePairOfstringanyType>
</a:Results>
</ExecuteResult>
</ExecuteResponse>
</s:Body>
</s:Envelope>
接下来,您需要解析此XML并使用CRM表单JavaScript将标签放置在所需的位置。您可以在//c:OptionSet/c:Options/c:OptionMetadata/c:Label/a:LocalizedLabels/a:Label tags 中找到选项名称。
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 当设置addFromAutocompleteOnly时,剩余文本将保留在输入字段中
- 使用jquery将输入字段转换为文本
- 如何从查询字符串中的输入字段发回文本
- 表单输入字段随着溢出的文本而增长
- 使用单个文本框向多个字段添加严格搜索
- 使用javascript提交表单并从字段/单选框/复选框/文本区域获取数据
- 使用jQuery的输入字段文本换行
- 向使用jQuery加载DOM后添加的字段添加不受限制的文本输入DatePickers
- 当用户单击按钮(在光标位置)时,在输入字段中添加一个文本字符串
- 如何根据文本长度立即显示和隐藏字段?-JQuery
- 如何使文本、数字和日期html输入字段以一致的方式支持Ctrl+Z(撤消)
- 模拟文本输入字段上的退格键
- 输入字段中的可持续文本
- 未获取文本输入字段的值
- 没有文本安全性的密码字段
- 将禁用的输出字段更改为带前缀的文本
- 为什么 react 将输入[类型=“文本”] 字段设为只读,除非我提供 onChange 回调
- 使用SAPUI5中的“文本”字段显示正确的“日期”
- Kendo,如何将MVVM文本:字段绑定到远程DataSource