首页 > 代码库 > Dynamics CRM OData 查询超过50条记录的数据(Retrieving More than 50 records using OData)

Dynamics CRM OData 查询超过50条记录的数据(Retrieving More than 50 records using OData)

在通过ODdata方式获取CRM数据时,默认查询出来的results只有50条数据,可以通过JSON返回的Object中的“_next”属性作为URL循环获取直到该属性为空


示例代码如下:

relatedAccounts = [];

function onl oad() {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/AccountSet?$select=AccountId,Name,&$filter=StateCode/Value eq 0";
    GetRecords(oDataUri);
    var totalRecords = relatedAccounts.length;
}

function GetRecords(url) {
    jQuery.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: url,
        async: false,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (data && data.d != null && data.d.results != null) {
                AddRecordsToArray(data.d.results);
                FetchRecordsCallBack(data.d);
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
            alert("Error :  has occured during retrieval of the records ");
        }
    });
}

function AddRecordsToArray(records) {
    for (var i = 0; i < records.length; i++) {
        relatedAccounts.push(records[i]);
    }
}

function FetchRecordsCallBack(records) {
    if (records.__next != null) {
        var url = records.__next;
        GetRecords(url);
    }
}

本文示例代码摘自博客:

http://lakshmanindian.wordpress.com/2012/12/07/retrieving-more-than-50-records-using-odata-in-crm-2011/