Разработка

SOAP-запросы для JavaScript

Примеры различных SOAP-запросов к CRM, подготовленных для выполнения с помощью JavaScript.

WhoAmI Request

var request = [
'<request i:type="b:WhoAmIRequest" xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:b="http://schemas.microsoft.com/crm/2011/Contracts">',
    '<a:Parameters xmlns:c="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />',
    '<a:RequestId i:nil="true" />',
    '<a:RequestName>WhoAmI</a:RequestName>',
'</request>'].join('');

RetrieveEntity Request

var entityName = 'account';
var entityFilters = ['Attributes', 'Entity' /*'All', 'Privileges', 'Relationships'*/].join(' ');
var retrieveAsIfPublished = false;
var request = [
'<request i:type="a:RetrieveEntityRequest" xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">',
    '<a:Parameters xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic">',
        '<a:KeyValuePairOfstringanyType>',
            '<b:key>EntityFilters</b:key>',
            '<b:value i:type="c:EntityFilters" xmlns:c="http://schemas.microsoft.com/xrm/2011/Metadata">' + entityFilters + '</b:value>',
        '</a:KeyValuePairOfstringanyType>',
        '<a:KeyValuePairOfstringanyType>',
            '<b:key>MetadataId</b:key>',
            '<b:value i:type="ser:guid" xmlns:ser="http://schemas.microsoft.com/2003/10/Serialization/">00000000-0000-0000-0000-000000000000</b:value>',
        '</a:KeyValuePairOfstringanyType>',
        '<a:KeyValuePairOfstringanyType>',
            '<b:key>RetrieveAsIfPublished</b:key>',
            '<b:value i:type="c:boolean" xmlns:c="http://www.w3.org/2001/XMLSchema">' + retrieveAsIfPublished.toString() + '</b:value>',
        '</a:KeyValuePairOfstringanyType>',
        '<a:KeyValuePairOfstringanyType>',
            '<b:key>LogicalName</b:key>',
            '<b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">' + entityName + '</b:value>',
        '</a:KeyValuePairOfstringanyType>',
    '</a:Parameters>',
    '<a:RequestId i:nil="true" />',
    '<a:RequestName>RetrieveEntity</a:RequestName>',
'</request>'].join('');

Достать данные из ответа можно так:

var response = ... // SOAP-ответ
$(response).find('c\\:AttributeMetadata').each(
    function(){
        var type = $(this).find('c\\:AttributeType').text();
        var name = $(this).find('c\\:LogicalName').text();
        if (type == 'Lookup' || type == 'Customer') {
            var targets = [];
            $(this).find('c\\:Targets d\\:string').each(function() { targets.push($(this).text()); });
        }
    }
);

About the author

Добавить комментарий