locked
CRM 2011: Get the lookuptypes via JavaScript (supported alternative?) RRS feed

  • Question

  • Hi, 

    I need to retrieve the lookup types (entities that could be referenced by the lookup) via JavaScript. I am currently using this littel function:

     

    function GetLookupTypes(lookupid) {
    
      // returns string (e.g.: "account:1:Account,contact:2:Contact" )
      var lookupTypesString = document.getElementById(lookupid).lookuptypenames;
      // returns array (e.g.: [account, 1, Account, contact, 2, Contact])
      var typesCollection = lookupTypesString.split(':');
    
      var values = [],
       // item consits of name, typecode, and displayname
       blocksize = 3;
    
      for (var i = 0, max = (typesCollection.length / blocksize); i < max; i++) {
    
       var position = (i * blocksize)
    
       var item = {
        entityname: typesCollection[position],
        entitytypecode: typesCollection[position + 1],
        entitydisplayname: typesCollection[position + 2]
       };
    
       values.push(item);
      }
    
      return values;
     };
    

     

    But I assume this is not a supported (document.getElementById  instead of Xrm.Page) implementation. So does anyone know a supported alternative to the code above? 

     

    Daniel


    Tuesday, August 30, 2011 9:54 AM

Answers

  • Daniel -

    This is a good question. What you have done is unsupported, but it works.

    The alternative is to query the attribute metadata. LookupAttributeMetadata attributes contain a Targets property with a string array containing the valid types of entities for that lookup.

    Unfortunately, I note that the sample library to retrieve Attribute metadata in Sample: Retrieve Entity Metadata using JScript doesn't retrieve it properly.

    At the very bottom of the SDK.MetaData.js file in that sample you will see it is setting that property to null.

    _lookupAttributeMetadata: function (node) {
     ///<summary>
     /// Private function that parses xml data describing LookupAttributeMetadata 
     ///</summary>
     var attributeMetadata = new SDK.MetaData._attributeMetadata(node);
     attributeMetadata.Targets = null;
    
     return attributeMetadata;
     },
    
    

    The correct implementation should be to return the array of strings that ought to be returned.

    This is what you need to change:

     

     _lookupAttributeMetadata: function (node) {
     ///<summary>
     /// Private function that parses xml data describing LookupAttributeMetadata 
     ///</summary>
     var attributeMetadata = new SDK.MetaData._attributeMetadata(node);
     var targetsNode = node.selectSingleNode("c:Targets");
     var arrTargets = [];
     for (var i = 0; i < targetsNode.childNodes.length; i++) {
     arrTargets.push(targetsNode.childNodes[i].text);
     }
     attributeMetadata.Targets = arrTargets;
    
     return attributeMetadata;
     },
    

    Here is an example of retrieving the Targets using the library:

     

     

     SDK.MetaData.RetrieveAttributeAsync("opportunity", "customerid", null, false,
     function (am) {
    
     alert(am.Targets.);
     },
     function () { });
    

     

     

    This is the server side code to retrieve it for the Opportunity customerid lookup

     

    				RetrieveAttributeRequest req = new RetrieveAttributeRequest()
    				{ EntityLogicalName="opportunity", LogicalName="customerid", RetrieveAsIfPublished= false};
    				RetrieveAttributeResponse customerLookup = (RetrieveAttributeResponse)_proxy.Execute(req);
    				
    				LookupAttributeMetadata lam = (LookupAttributeMetadata)customerLookup.AttributeMetadata;
    				foreach(String entitylogicalName in lam.Targets)
    				{ Console.WriteLine(entitylogicalName);}
    

    It returns 'account' and 'contact'.

    I also think the Xrm.Page object model should have a method called 'getTargets' just for lookup attributes so that you don't need to retrieve them from metadata.

    I'll log a bug, but for anyone else who needs this, be sure to go to the Connect site and request this. The more people who ask, the more likely it will make it in in the future.

    I hope this helps.

    Best regards

     

     


    Jim Daly Technical Writer Microsoft Dynamics CRM


    Wednesday, August 31, 2011 11:39 PM
    Answerer

All replies

  • Hello Daniel,

    In the case SDK doesn't have description how to do it (as it is) you would not have other possibility get your answer.

     

    Cornel answer please - how does your url can help this visitor with his issue? I have checked and haven't found anything helpful regarding this question. So please try not to post answers which doesn't contain  answer but leads to your website.


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter
    Wednesday, August 31, 2011 10:42 PM
    Moderator
  • Daniel -

    This is a good question. What you have done is unsupported, but it works.

    The alternative is to query the attribute metadata. LookupAttributeMetadata attributes contain a Targets property with a string array containing the valid types of entities for that lookup.

    Unfortunately, I note that the sample library to retrieve Attribute metadata in Sample: Retrieve Entity Metadata using JScript doesn't retrieve it properly.

    At the very bottom of the SDK.MetaData.js file in that sample you will see it is setting that property to null.

    _lookupAttributeMetadata: function (node) {
     ///<summary>
     /// Private function that parses xml data describing LookupAttributeMetadata 
     ///</summary>
     var attributeMetadata = new SDK.MetaData._attributeMetadata(node);
     attributeMetadata.Targets = null;
    
     return attributeMetadata;
     },
    
    

    The correct implementation should be to return the array of strings that ought to be returned.

    This is what you need to change:

     

     _lookupAttributeMetadata: function (node) {
     ///<summary>
     /// Private function that parses xml data describing LookupAttributeMetadata 
     ///</summary>
     var attributeMetadata = new SDK.MetaData._attributeMetadata(node);
     var targetsNode = node.selectSingleNode("c:Targets");
     var arrTargets = [];
     for (var i = 0; i < targetsNode.childNodes.length; i++) {
     arrTargets.push(targetsNode.childNodes[i].text);
     }
     attributeMetadata.Targets = arrTargets;
    
     return attributeMetadata;
     },
    

    Here is an example of retrieving the Targets using the library:

     

     

     SDK.MetaData.RetrieveAttributeAsync("opportunity", "customerid", null, false,
     function (am) {
    
     alert(am.Targets.);
     },
     function () { });
    

     

     

    This is the server side code to retrieve it for the Opportunity customerid lookup

     

    				RetrieveAttributeRequest req = new RetrieveAttributeRequest()
    				{ EntityLogicalName="opportunity", LogicalName="customerid", RetrieveAsIfPublished= false};
    				RetrieveAttributeResponse customerLookup = (RetrieveAttributeResponse)_proxy.Execute(req);
    				
    				LookupAttributeMetadata lam = (LookupAttributeMetadata)customerLookup.AttributeMetadata;
    				foreach(String entitylogicalName in lam.Targets)
    				{ Console.WriteLine(entitylogicalName);}
    

    It returns 'account' and 'contact'.

    I also think the Xrm.Page object model should have a method called 'getTargets' just for lookup attributes so that you don't need to retrieve them from metadata.

    I'll log a bug, but for anyone else who needs this, be sure to go to the Connect site and request this. The more people who ask, the more likely it will make it in in the future.

    I hope this helps.

    Best regards

     

     


    Jim Daly Technical Writer Microsoft Dynamics CRM


    Wednesday, August 31, 2011 11:39 PM
    Answerer
  • Hello Jim & Andriy, 

    thanks for the answers. Ok the apporache with the meta-data service is a supported alertnative but I think i go with the unsupported approach for the moment. 

     

    Daniel

    Thursday, September 1, 2011 10:05 AM