0

我有一个包含 2 个自定义字段的表单:“选项集”和“多行文本”。我将 JavaScript 文件作为 WebResource 上传,并将该函数添加到 Form 的 Onload 事件中。我想知道如何使用 JavaScript 将所有选项集项目复制到多行文本框。

4

1 回答 1

0

尝试使用此代码片段:此请求获取选项集的元数据并以 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 标记中找到选项名称。

于 2013-09-20T07:46:23.617 回答