首页 > 代码库 > crm使用soap批量删除数据

crm使用soap批量删除数据

//批量删除数据
function demo() {
    //实体名称
    var entityname = "fw_student";
    var data = http://www.mamicode.com/[];
    data.push("8B1D93C6-BF1E-E411-9406-00155D005802");
    data.push("20718494-0C2C-E411-9407-00155D005802");

    executemultipleDelete(entityname, data);
}

function executemultipleDelete(name,iddata) {
    var request = "<s:Envelope xmlns:s=‘http://schemas.xmlsoap.org/soap/envelope/‘>";
    request += "<s:Body>";
    request += "<Execute xmlns=‘http://schemas.microsoft.com/xrm/2011/Contracts/Services‘ xmlns:i=‘http://www.w3.org/2001/XMLSchema-instance‘>";
    request += "<request i:type=‘a:ExecuteMultipleRequest‘ xmlns:a=‘http://schemas.microsoft.com/xrm/2011/Contracts‘>";
    request += "<a:Parameters xmlns:b=‘http://schemas.datacontract.org/2004/07/System.Collections.Generic‘>";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<b:key>Requests</b:key>";
    request += "<b:value i:type=‘c:OrganizationRequestCollection‘ xmlns:c=‘http://schemas.microsoft.com/xrm/2012/Contracts‘>";
    var len = iddata.length;
    for (var i = 0; i < len; i++) {
        addItem(name,iddata[i]);
    }
    request += "</b:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<b:key>Settings</b:key>";
    request += "<b:value i:type=‘c:ExecuteMultipleSettings‘ xmlns:c=‘http://schemas.microsoft.com/xrm/2012/Contracts‘>";
    request += "<c:ContinueOnError>false</c:ContinueOnError>";
    request += "<c:ReturnResponses>true</c:ReturnResponses>";
    request += "</b:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "</a:Parameters>";
    request += "<a:RequestId i:nil=‘true‘ />";
    request += "<a:RequestName>ExecuteMultiple</a:RequestName>";
    request += "</request>";
    request += "</Execute>";
    request += "</s:Body>";
    request += "</s:Envelope>";

    execSoap(request);
}

function addItem(name,id) {
    var request = "<c:OrganizationRequest i:type=‘a:DeleteRequest‘>";
    request += "<a:Parameters>";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<b:key>Target</b:key>";
    request += "<b:value i:type=‘a:EntityReference‘>";
    request += "<a:Id>" + id + "</a:Id>";
    request += "<a:LogicalName>"+ name +"</a:LogicalName>";
    request += "<a:Name i:nil=‘true‘ />";
    request += "</b:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "</a:Parameters>";
    request += "<a:RequestId i:nil=‘true‘ />";
    request += "<a:RequestName>Delete</a:RequestName>";
    request += "</c:OrganizationRequest>";

    return request;
}

//获取服务地址
function getWebUrl() {
    var serverUrl = Xrm.Page.context.getServerUrl();
    if (serverUrl.match(/\/$/)) {
        serverUrl = serverUrl.substring(0, serverUrl.length - 1);
    }
    return serverUrl + "/XRMServices/2011/Organization.svc/web";
}
//执行请求
function execSoap(request) {
    var ajaxRequest = new XMLHttpRequest();
    ajaxRequest.open("POST", getWebUrl(), true)
    ajaxRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
    ajaxRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    ajaxRequest.setRequestHeader("SOAPAction", "
http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
    ajaxRequest.send(request);
}

crm使用soap批量删除数据