2017-09-10 1 views
1

Ich denke, dieser Code würde auf einem Formular funktionieren. Ich habe immer Probleme mit Xrm.Page.Context und Methoden wie getServerUrl, wenn ich an Javascript innerhalb einer HTML-Web-Ressource arbeite.Uncaught TypeError: u (...). GetServerUrl ist keine Funktion in HTML-Web-Ressource

Hier wird die Funktion Ich versuche zu laufen:

function getAttributeOptions(entityLogicalName, attributeLogicalName, dictionaryObject, onComplete) { 
 
    ///<summary> 
 
    /// <para>Retrieves the picklist attribute options and appends them to a dictionary object.</para> 
 
    /// <para>It then executes the function assigned to the onComplete parameter.</para> 
 
    ///</summary> 
 
    ///<param name="entityLogicalName" type="String"> 
 
    /// The logical name of the entity 
 
    ///</param> 
 
    ///<param name="attributeLogicalName" type="String"> 
 
    /// The logical name of the Picklist attribute 
 
    ///</param> 
 
    ///<param name="dictionaryObject" type="Object"> 
 
    /// An empty object that will become a dictionary for the option values. 
 
    ///</param> 
 
    ///<param name="onComplete" type="Function"> 
 
    /// A function to perform when the options are assigned to the dictionaryObject; 
 
    ///</param> 
 
    if (!(typeof entityLogicalName == "string")) { 
 
     throw new Error("getAttributeOptions entityLogicalName parameter is required and must be a string."); 
 
    } 
 
    if (!(typeof attributeLogicalName == "string")) { 
 
     throw new Error("getAttributeOptions attributeLogicalName parameter is required and must be a string."); 
 
    } 
 
    if (!(typeof dictionaryObject == "object")) { 
 
     throw new Error("getAttributeOptions dictionaryObject parameter is required and must be an object."); 
 
    } 
 
    if (!(typeof onComplete == "function")) { 
 
     throw new Error("getAttributeOptions onComplete parameter is required and must be a function."); 
 
    } 
 

 
    var passThroughObject = {}; 
 
    passThroughObject.eln = entityLogicalName; 
 
    passThroughObject.aln = attributeLogicalName; 
 
    passThroughObject.dObj = dictionaryObject; 
 
    passThroughObject.oc = onComplete; 
 

 
    if ((typeof SDK == "undefined") || (typeof SDK.Metadata == "undefined") || (typeof SDK.Metadata.Query == "undefined")) { 
 
     throw new Error("getAttributeOptions function requires the SDK.Metadata.Query.min.js library and it is not present."); 
 
    } 
 

 
    var mdq = SDK.Metadata.Query; 
 
    var semp = mdq.SearchableEntityMetadataProperties; 
 
    var samp = mdq.SearchableAttributeMetadataProperties; 
 
    var srmp = mdq.SearchableRelationshipMetadataProperties 
 
    var emp = mdq.EntityMetadataProperties; 
 
    var amp = mdq.AttributeMetadataProperties; 
 
    var rmp = mdq.RelationshipMetadataProperties; 
 
    var ve = mdq.ValueEnums; 
 

 
    //EntityFilter 
 
    var ef = new mdq.MetadataFilterExpression(mdq.LogicalOperator.And); 
 
    ef.addCondition(semp.LogicalName, mdq.MetadataConditionOperator.Equals, entityLogicalName); 
 
    //Entity Properties 
 
    var ep = new mdq.MetadataPropertiesExpression(false, [emp.Attributes]); 
 

 
    //Attribute Filter 
 
    var af = new mdq.MetadataFilterExpression(mdq.LogicalOperator.And); 
 
    af.addCondition(samp.LogicalName, mdq.MetadataConditionOperator.Equals, attributeLogicalName); 
 

 
    //Attribute Properties 
 
    var ap = new mdq.MetadataPropertiesExpression(false, [amp.OptionSet, amp.AttributeType]); 
 
    // AttributeQuery 
 
    var aq = new mdq.AttributeQueryExpression(af, ap); 
 
    // LabelQuery 
 
    var lq = new mdq.LabelQueryExpression([1033]); 
 
    //EntityQueryExpression 
 
    var eqe = new mdq.EntityQueryExpression(ef, ep, aq, null, lq); 
 
    //RetrieveMetadataChangesRequest 
 
    var rmcr = new mdq.RetrieveMetadataChangesRequest(eqe, null, mdq.DeletedMetadataFilters.Default); 
 

 
    mdq.RetrieveMetadataChanges(
 
     rmcr, 
 
     function(rmcResponse, pto) { 
 
      var attributeType = "unknown"; 
 
      if (typeof rmcResponse.EntityMetadata[0] == "undefined" || rmcResponse.EntityMetadata[0] == null) { 
 
       throw new Error("No entity metadata found for " + pto.eln); 
 
      } 
 
      if (typeof rmcResponse.EntityMetadata[0].Attributes[0] == "undefined" || rmcResponse.EntityMetadata[0].Attributes[0] == null) { 
 
       throw new Error("No attribute metadata found for " + pto.eln + "." + pto.aln); 
 
      } 
 
      if (typeof rmcResponse.EntityMetadata[0].Attributes[0].OptionSet == "undefined" || rmcResponse.EntityMetadata[0].Attributes[0].OptionSet == null) { 
 
       throw new Error(pto.eln + "." + pto.aln + " does not have an OptionSet property."); 
 
      } 
 
      attributeType = rmcResponse.EntityMetadata[0].Attributes[0].AttributeType; 
 
      if (attributeType != "Boolean") { 
 
       if (typeof rmcResponse.EntityMetadata[0].Attributes[0].OptionSet.Options == "undefined" || rmcResponse.EntityMetadata[0].Attributes[0].OptionSet.Options == null) { 
 
        throw new Error(pto.eln + "." + pto.aln + "OptionSet does not have an Options property"); 
 
       } 
 
       if (typeof rmcResponse.EntityMetadata[0].Attributes[0].OptionSet.Options[0] == "undefined" || rmcResponse.EntityMetadata[0].Attributes[0].OptionSet.Options[0] == null) { 
 
        throw new Error(pto.eln + "." + pto.aln + "OptionSet.Options does not have any options"); 
 
       } 
 
      } 
 

 

 
      pto.dObj[pto.eln] = pto.dObj[pto.eln] || {}; 
 
      pto.dObj[pto.eln].attributes = pto.dObj[pto.eln].attributes || {}; 
 
      pto.dObj[pto.eln].attributes[pto.aln] = pto.dObj[pto.eln].attributes[pto.aln] || {}; 
 
      pto.dObj[pto.eln].attributes[pto.aln].options = pto.dObj[pto.eln].attributes[pto.aln].options || {}; 
 
      if (attributeType != "Boolean") { 
 
       for (var i in rmcResponse.EntityMetadata[0].Attributes[0].OptionSet.Options) { 
 
        var option = rmcResponse.EntityMetadata[0].Attributes[0].OptionSet.Options[i]; 
 
        pto.dObj[pto.eln].attributes[pto.aln].options[option.Value] = option.Label.UserLocalizedLabel.Label; 
 
       } 
 
      } else { 
 
       pto.dObj[pto.eln].attributes[pto.aln].options[0] = rmcResponse.EntityMetadata[0].Attributes[0].OptionSet.FalseOption.Label.UserLocalizedLabel.Label; 
 
       pto.dObj[pto.eln].attributes[pto.aln].options[1] = rmcResponse.EntityMetadata[0].Attributes[0].OptionSet.TrueOption.Label.UserLocalizedLabel.Label; 
 
      } 
 

 
      pto.oc(pto.eln, pto.aln); 
 
     }, 
 
     function(error) { 
 
      writeMessage(error.message); 
 
     }, 
 
     passThroughObject); 
 

 
} 
 

 
// MetaDataDictionary 
 
var mdd = {}; 
 

 
function testResults(entityLogicalName, attributeLogicalName) { 
 
    alert("Options for " + entityLogicalName + "." + attributeLogicalName + " cached."); 
 
} 
 
getAttributeOptions("contact", "statecode", mdd, testResults); 
 

 
for (option in mdd.contact.attributes.statecode.options) { 
 
    var value = option; 
 
    var label = mdd.contact.attributes.statecode.options[option]; 
 
    alert(label); 
 
}

Hier ist der problematische Teil der Bibliothek, die ich benutze:

function u() { 
 
      var n = "Context is not available."; 
 
      return typeof GetGlobalContext != "undefined" ? GetGlobalContext() : typeof Xrm != "undefined" ? Xrm.Page.context : new Error(n) 
 
     } 
 

 
     function h() { 
 
      var n = u().getServerUrl(); 
 
      return n.match(/\/$/) && (n = n.substring(0, n.length - 1)), typeof u().getClientUrl != "undefined" && (n = u().getClientUrl()), n 
 
     }

Hier ist mein Fehler:

enter image description here

Ich würde wirklich wie für getServerUrl und getClientUrl mir zur Verfügung stehen. Was kann ich tun, um dies zu erreichen?

Antwort

1

Überprüfen Sie den Pfad & Fügen Sie diese Bibliothek in Ihrer HTML-Web-Ressource hinzu, um auf den CRM-Kontext zuzugreifen.

<script type="text/javascript" src="ClientGlobalContext.js.aspx"></script> 

When you need context information outside a form, include a reference to the ClientGlobalContext.js.aspx page in an HTML web resource.

GetGlobalContext

Auch getServerUrl ist deprecated Sie getClientUrl auf der Grundlage Ihrer CRM-Version verwenden.

+1

Das sieht sehr vielversprechend aus. Die Bibliothek, die ich verwendete, war jedoch SDK.Metadata.Query.min.js. Es stellt sich heraus, dass ein Großteil dieser Bibliothek abgeschrieben ist, aber ich musste hier einige Absätze lesen https://msdn.microsoft.com/en-us/library/jj919080(v=crm.6).aspx, um das zu realisieren. Am Ende habe ich diese komplizierte Web-API-Anfrage verwendet, um die gewünschten Daten zu erhalten: http: //clienturl/api/data/v8.2/EntityDefinitions (608861bc-50a4-4c5f-a02c-21fe1943e2cf) /Attributes/Microsoft.Dynamics. CRM.StateAttributeMetadata? $ Select = LogischerName & $ expand = OptionSet, GlobalOptionSet Ich werde versuchen, Ihre Antwort bald zu bestätigen. –

Verwandte Themen