MS CRM 2011 在JScript中同步和异步使用REST Endpoint

 

原创地址:http://www.cnblogs.com/jfzhu/admin/EditPosts.aspx?postid=2910622

转载请注明出处

 

在JScript中可以同步,也可以异步地使用REST Endpoint。以Retrieve为例,在下面的代码中,retrieveRecordsReq.open("GET", oDataPath + filter, false) 的第二个参数是false即为同步调用,如果为true即为异步调用。代码中其他部分同步与异步完全相同。

if (typeof (ContactReadPaneLibrary) == "undefined") {
    ContactReadPaneLibrary = { __namespace: true };
}


ContactReadPaneLibrary = {
    Name: "ContactReadPaneLibrary",
    OnLoad: function () {
        if (parent.window.Xrm.Page.getAttribute("primarycontactid").getValue() != null && parent.window.Xrm.Page.getAttribute("primarycontactid").getValue().length > 0) {
            var primartycontact = parent.window.Xrm.Page.getAttribute("primarycontactid").getValue()[0];
            var contactid = primartycontact.id;
            var oDataPath = Xrm.Page.context.prependOrgName("/xrmservices/2011/organizationdata.svc");
            var filter = "/ContactSet(guid'" + contactid + "')?$select=Telephone1,FullName";
            var retrieveRecordsReq = new XMLHttpRequest();
            retrieveRecordsReq.open("GET", oDataPath + filter, false);
            retrieveRecordsReq.setRequestHeader("Accept", "application/json");
            retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            retrieveRecordsReq.onreadystatechange = function () {
                ContactReadPaneLibrary.RetrieveContactCallBack(this);
            };
            retrieveRecordsReq.send();
        }
    },
    RetrieveContactCallBack: function (retrieveRecordsReq) {
        if (retrieveRecordsReq.readyState == 4 /* complete */) {
            if (retrieveRecordsReq.status == 200) {
                //Success
                //alert(retrieveRecordsReq.responseText);
                if (retrieveRecordsReq.responseText) {
                    var retrievedRecord = JSON.parse(retrieveRecordsReq.responseText).d;
                    document.getElementById("TextFullname").value = retrievedRecord.FullName;
                    document.getElementById("TextBusinessPhone").value = retrievedRecord.Telephone1;
                }
            }
            else {
                alert("Error : " + retrieveRecordsReq.status + ": " +
                    retrieveRecordsReq.statusText + ": " +
                    JSON.parse(retrieveRecordsReq.responseText).error.message.value);
            }
        }
    }
}

 

 

posted @ 2013-02-12 22:39  Coding十日谈  阅读(530)  评论(0编辑  收藏