Получение атрибутов объекта по GUID - PullRequest
0 голосов
/ 07 июля 2011

У меня есть форма, которая содержит поиск контакта. Поэтому я могу получить имя, фамилию и имя контакта.

Этот код:

var temp =  crmForm.all.to.DataValue;
alert(temp[0].id + "\n" + temp[0].name + "\n" + temp[0].typename); 

возвращает действительные идентификатор, имя и тип.

Как я могу получить атрибуты этого контакта (номера телефонов в данном случае) с этой информацией? Я пытаюсь сделать это в функции OnLoad формы, поэтому мне нужно сделать это в JavaScript.

1 Ответ

1 голос
/ 14 июля 2011

Вы можете использовать следующий метод для вызова веб-сервисов

function GetObjectAttribute(objectid, entityname, attribute) {
    // Preparer the SOAP message
    var message =
        [
        "<?xml version='1.0' encoding='utf-8'?>",
        "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'",
        " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'",
        " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>",
        GenerateAuthenticationHeader(),
        "<soap:Body>",
        "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>",
        "<entityName>",
        entityname,
        "</entityName>",
        "<id>",
        objectid,
        "</id>",
        "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'",
        " xsi:type='q1:ColumnSet'>",
        "<q1:Attributes><q1:Attribute>",
        attribute,
        "</q1:Attribute></q1:Attributes>",
        "</columnSet></Retrieve>",
        "</soap:Body></soap:Envelope>"
        ].join("");

    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    xmlhttp.open("POST", "/MSCrmServices/2007/CrmService.asmx", false);
    xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
    xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlhttp.setRequestHeader("Content-Length", message.length);
    xmlhttp.send(message);

    var result = xmlhttp.responseXML;
    var error = result.selectNodes('//error').length;
    if (error == 0) {
        var attributeNode = result.selectSingleNode('//q1:' + attribute);
        if (attributeNode != null) {
            return attributeNode.text;
        }
    }
    return null;
}

использование

var fullname = GetObjectAttribute(<GUID>, "Contacts", "fullname");
...