Daren's profileDaren TurnerBlogLists Tools Help

Blog


    December 15

    How to pull entity information from the Microsoft CRM MetaData Service using javascript.

     

    The following code will pull entity information, using the Metadata service, using javascript.  This code is useful for pulling the objecttypecode dynamically when they may differ between environments.

    var _sWebServicesNamespace = "http://schemas.microsoft.com/crm/2006/WebServices";

    function _RemoteMetaCommand(sCommand, sAction)
    {
    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    xmlhttp.open("POST", "/mscrmservices/2006/metadataservice.asmx" , false);
    xmlhttp.setRequestHeader("Content-Type","text/xml; charset=utf-8");
    xmlhttp.setRequestHeader("SOAPAction", _sWebServicesNamespace + "/" + sAction);

    var soapmessage = "<?xml version='1.0' encoding='utf-8'?>"
    soapmessage += "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
    soapmessage += "<soap:Body>" + sCommand + "</soap:Body></soap:Envelope>"
    xmlhttp.send(soapmessage);

    return xmlhttp.responseXML;
    }

    var entity = "new_country"
    var query = "<RetrieveEntityMetadata xmlns='http://schemas.microsoft.com/crm/2006/WebServices'>"
    query += "<entityName>" + entity + "</entityName><flags>EntityOnly</flags></RetrieveEntityMetadata>"

    //Get ObjectTypeCode

    var result = _RemoteMetaCommand(query, "RetrieveEntityMetadata").selectSingleNode("//ObjectTypeCode");

    var countryentitytypecode = result.text;

     

    Here is the response.

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://dtu11.spaces.live.com/blog/cns!D01B965C27CB1CC6!211.trak
    Weblogs that reference this entry
    • None