共用方式為


範例:使用 OData 端點和 JavaScript 關聯及解除關聯記錄

 

發行︰ 2016年11月

適用於: Dynamics CRM 2015

這個範例程式碼適用於 Microsoft Dynamics CRM 2015 和 Microsoft Dynamics CRM Online 2015 更新。下載 Microsoft Dynamics CRM SDK 套件。 可以在下列位置的下載套件中找到:

SampleCode\JS\RESTEndpoint\JavaScriptRESTAssociateDisassociate

如果只是想要了解此範例運作方式,可以安裝 (匯入) JavaScriptRESTAssociateDisassociateSample_1_0_0_2_managed.zip 受管理解決方案 (包含在下載檔案中)。 如果您安裝此受管理的解決方案且要使用此主題中的名稱建立 Web 資源,您的解決方案發行者自訂的首碼不可以是「sample」,除非您解除安裝 (刪除) 受管理的解決方案。

需求

若要執行範例程式碼,建立下列 Web 資源:

  • sample_/JavaScriptRESTAssociateDisassociate.htm
    提供使用者介面執行範例的 HTML 頁面。

  • sample_/Scripts/JavaScriptRESTAssociateDisassociate.js
    包含函數管理使用者介面的 JavaScript 程式庫。

  • sample_/Scripts/SDK.REST.js
    此 JavaScript 程式庫提供 SDK.REST.associateRecordsSDK.REST.disassociateRecords 函數,以及用於使用 Web 資源 REST 端點的其他函數。

注意

自訂首碼 ‘sample_’ 並未用於程式碼。 利用任何發行者的自訂首碼,這些範例都會運作。 不過已模擬 Scripts 資料夾的相對路徑必須包含在 Web 資源的名稱中。

示範

此範例包含兩種範例:

  • 關聯客戶
    此範例建立兩個新的客戶記錄以及關聯它們。 下列步驟說明動作:

    1. 建立名為「上層客戶」的新客戶記錄。

    2. 建立名為「下層客戶」的新客戶記錄。

    3. 關聯記錄並設定上層客戶做為下層客戶的上層。

    4. 顯示連結,可讓您確認關聯執行。

    5. 顯示按鈕,可讓您解除關聯及刪除範例建立的記錄。

  • 將客戶關聯到電子郵件做為 ActivityParty
    此範例建立 account 和 email 記錄,然後使用 activityparty 記錄,將客戶關聯到電子郵件做為 from 欄位。 下列步驟說明動作:

    1. 建立名為「電子郵件寄件者客戶」的新客戶記錄。

    2. 建立名為「電子郵件活動」的新電子郵件活動。

    3. 建立 activityparty 記錄,將客戶關聯到電子郵件。

    4. 顯示連結,可讓您刪除記錄。

    5. 顯示按鈕,可讓您刪除記錄。

這兩個範例都有啟動範例的按鈕。 結果在頁面底部的 [結果] 區段中顯示。 在執行任一個範例之後,請使用 [REST 範例] 按鈕再次執行範例。

sample_/JavaScriptRESTAssociateDisassociate.htm

此檔案提供 UI,說明範例行為以及開始並重新設定範例的按鈕。


<html lang="en-US">
<head>
 <meta http-equiv="X-UA-Compatible" content="IE=edge" />
 <title>JavaScript REST Associate and Disassociate Sample</title>
 <script src="../ClientGlobalContext.js.aspx" type="text/javascript"></script>
 <script src="Scripts/SDK.REST.js" type="text/javascript"></script>
 <script src="Scripts/JavaScriptRESTAssociateDisassociate.js" type="text/javascript"></script>
</head>
<body style="background-color: White; font-family: Segoe UI; font-size: small;">
 <h1 style="font-size: small;">
  Associating and Disassociating Records using JavaScript and the REST Endpoint for Web Resources</h1>
 <p>
  This page requires JavaScript and will update dynamically.</p>
 <p>
  <input id="dispalert" name="dispalert" type="checkbox" value="alert" /><label for="dispalert">Display alert window when data changes.</label></p>
 <table width="100%" summary="This table displays 2 samples which will demonstrate associating records using JavaScript and the REST Endpoint for Web Resources.">
  <thead>
   <tr>
    <th scope="col">
     Associate Accounts Sample
    </th>
    <th scope="col">
     Associate Account to Email as ActivityParty Sample
    </th>
   </tr>
  </thead>
  <tr>
   <td>
    <p>
     <label for="btnStartAssociateAccounts">
      Click the <b>Start Associate Accounts Sample</b> button to start the sample showing how to associate two account records.</label></p>
    <button id="btnStartAssociateAccounts" title="Start the Associate Accounts Sample">
     Start Associate Accounts Sample</button><br />
    <p>
     This sample will:</p>
    <ol>
     <li>Create a new account record named "Parent Account".</li>
     <li>Create a new account record named "Child Account".</li>
     <li>Associate the records setting the "Parent Account" as the Parent Account of the "Child Account".</li>
     <li>Display links to allow you to verify that the association was done.</li>
     <li>Display a button to allow you to disassociate the records.
      <ul>
       <li>If you choose this option the sample will display links to allow you to verify that the records are no longer associated.</li>
      </ul>
     </li>
     <li>Display a button to allow you to delete the records.
      <ul>
       <li>If you choose this option the sample will display a message when the records are deleted.</li>
      </ul>
     </li>
    </ol>
   </td>
   <td valign="top" style="border-left: 1px solid black; padding-left: 5px;">
    <p>
     Click the <b>Start Associate Account to Email as ActivityParty Sample</b> button to start the sample showing how to associate an account record to an activity using ActivityParty.</p>
    <button id="btnStartAssociateAccountToEmail" title="Start the Associate Account to Email as ActivityParty sample.">
     Start Associate Account to Email as ActivityParty Sample</button>
    <p>
     This sample will:</p>
    <ol>
     <li>Create a new account record named "Email Sender Account".</li>
     <li>Create a new email activity record named "Email Activity".</li>
     <li>Create a new activity party record that associates the "Email Sender Account" to the <b>From:</b> field of the "Email Activity".</li>
     <li>Display links to allow you to verify that the association was done.</li>
     <li>Display a button to allow you to delete the records.
      <ul>
       <li>If you choose this option the sample will display a message when the records are deleted.</li>
      </ul>
     </li>
    </ol>
   </td>
  </tr>
 </table>


 <p>
  <label for="btnResetSample">
   After running one of the samples, click the <b>Reset Sample</b> button to reset the sample.</label></p>
 <button id="btnResetSample" title="Reset the sample" disabled="disabled">
  Reset Sample</button>
 <h2 style="font-size: small;">
  Results:</h2>
 <hr />
 <ol id="output">
 </ol>
</body>
</html>

sample_/Scripts/JavaScriptRESTAssociateDisassociate.js

此 JavaScript 程式庫提供管理使用者介面動作的函數。


/// <reference path="SDK.REST.js" />
var btnStartAssociateAccounts,
btnStartAssociateAccountToEmail,
btnDisassociateAccounts,
btnResetSample,
parentAccount,
childAccount,
accountRelatedToEmail,
emailRecord,
output, alertFlag;

document.onreadystatechange = function () {
 ///<summary>
 /// Initializes the sample when the document is ready
 ///</summary>
 if (document.readyState == "complete") {

  btnStartAssociateAccounts = document.getElementById("btnStartAssociateAccounts");
  btnStartAssociateAccountToEmail = document.getElementById("btnStartAssociateAccountToEmail");
  btnResetSample = document.getElementById("btnResetSample");
  alertFlag = document.getElementById("dispalert");
  output = document.getElementById("output");

  btnStartAssociateAccounts.onclick = startAssociateAccounts;
  btnStartAssociateAccountToEmail.onclick = startAssociateAccountToEmail;
  btnResetSample.onclick = resetSample;
 }
}

function startAssociateAccounts() {
 ///<summary>
 /// Creates two account records asynchronously and displays a message showing the Id values of the created records
 /// Then calls the associateAccounts function to associate them.
 ///</summary>
 disableElement(btnStartAssociateAccounts);
 disableElement(btnStartAssociateAccountToEmail);
 //Create the first account record
 SDK.REST.createRecord({ Name: "Parent Account", Description: "This Account will be the parent account" },
    "Account",
    function (account) {
     parentAccount = account;
     writeMessage("Account Id: {" + account.AccountId + "} Name: \"" + account.Name + "\" created as the parent account.");
     // Create the second account record
     SDK.REST.createRecord({ Name: "Child Account", Description: "This Account will be the child account" },
        "Account",
        function (account) {
         childAccount = account;
         writeMessage("Account Id: {" + account.AccountId + "} Name: \"" + account.Name + "\" created as the child account.");
         // Associate the accounts that were created.
         associateAccounts(parentAccount, childAccount);
        },
     errorHandler);
    },
     errorHandler);

 scrollToResults();
}

function associateAccounts(parentAccount, childAccount) {
 ///<summary>
 /// Associates two account records asynchronously and displays messages so you can verify the association
 /// Displays  buttons to disassociate or delete the accounts.
 /// Enables the reset button
 ///</summary>
 SDK.REST.associateRecords(parentAccount.AccountId,
    "Account",
    "Referencedaccount_parent_account",
    childAccount.AccountId,
    "Account",
    function () {
     writeMessage("Association successful.");
     showLinksToVerifyAccountAssociation();
     showButtonToDisassociateAccounts();
     showButtonToDeleteAccounts();
     enableElement(btnResetSample);
     if (alertFlag.checked == true)
      alert("Association successful.");
    },
    errorHandler);
}

function getUrl()
{
    var context;
    var url;
    if (typeof GetGlobalContext != "undefined")
    { context = GetGlobalContext(); }
    else
    {
        if ((typeof Xrm != "undefined") &amp;&amp; (typeof Xrm.Page != "undefined") &amp;&amp; (typeof Xrm.Page.context != "undefined"))
        {
            context = Xrm.Page.context;
        }
        else
        { throw new Error("Context is not available."); }
    }


        url = context.getClientUrl();


    return url;
}

function showLinksToVerifyAccountAssociation() {
 ///<summary>
 /// Displays links to verify the associated accounts.
 ///</summary>
 var message = document.createElement("div");
 var message1 = document.createElement("span");
 setText(message, "Open the ");
 var childRecordlink = document.createElement("a");
 childRecordlink.href = getUrl() + "/main.aspx?etn=account&amp;pagetype=entityrecord&amp;id=%7B" + childAccount.AccountId + "%7D";
 childRecordlink.target = "_blank";
 setText(childRecordlink, childAccount.Name + " record");
 var message2 = document.createElement("span");
 setText(message2, " to verify that the Parent Account field is set to the ");
 var parentRecordlink = document.createElement("a");
 parentRecordlink.href = getUrl() + "/main.aspx?etn=account&amp;pagetype=entityrecord&amp;id=%7B" + parentAccount.AccountId + "%7D";
 parentRecordlink.target = "_blank";
 setText(parentRecordlink, parentAccount.Name + " record");
 period = document.createElement("span");
 setText(period, ".");
 message.appendChild(message1);
 message.appendChild(childRecordlink);
 message.appendChild(message2);
 message.appendChild(parentRecordlink);
 message.appendChild(period);
 writeMessage(message);
}

function showLinksToVerifyDisAssociation() {
 ///<summary>
 /// Displays links to verify accounts were disassociated
 ///</summary>
 var message = document.createElement("div");
 var message1 = document.createElement("span");
 setText(message, "Open the ");
 var childRecordlink = document.createElement("a");
 childRecordlink.href = getUrl() + "/main.aspx?etn=account&amp;pagetype=entityrecord&amp;id=%7B" + childAccount.AccountId + "%7D";
 childRecordlink.target = "_blank";
 setText(childRecordlink, childAccount.Name + " record");
 var message2 = document.createElement("span");
 setText(message2, " to verify that the Parent Account field not set to the ");
 var parentRecordlink = document.createElement("a");
 parentRecordlink.href = getUrl() + "/main.aspx?etn=account&amp;pagetype=entityrecord&amp;id=%7B" + parentAccount.AccountId + "%7D";
 parentRecordlink.target = "_blank";
 setText(parentRecordlink, parentAccount.Name + " record");
 period = document.createElement("span");
 setText(period, ".");
 message.appendChild(message1);
 message.appendChild(childRecordlink);
 message.appendChild(message2);
 message.appendChild(parentRecordlink);
 message.appendChild(period);
 writeMessage(message);
}

function showButtonToDeleteAccounts() {
 ///<summary>
 /// Diplays a button to allow for deletion of accounts created for this sample.
 ///</summary>
 var btnDeleteRecords = document.createElement("button");
 setText(btnDeleteRecords, "Delete both records");
 btnDeleteRecords.title = "Delete both records";
 btnDeleteRecords.onclick = function () {
  disableElement(btnDisassociateAccounts);
  disableElement(this);
  SDK.REST.deleteRecord(childAccount.AccountId,
            "Account",
            function () { writeMessage(childAccount.Name + " record deleted."); },
            errorHandler);
  SDK.REST.deleteRecord(parentAccount.AccountId,
            "Account",
            function () {
             writeMessage(parentAccount.Name + " record deleted.");
             if (alertFlag.checked == true)
              alert("Records deleted.");
            },
            errorHandler);
 }
 writeMessage(btnDeleteRecords);
}

function showButtonToDisassociateAccounts() {
 ///<summary>
 /// Displays a button to allow for disassociation of accounts associated by this sample.
 /// Then shows links to verify disassociation
 ///</summary>
 btnDisassociateAccounts = document.createElement("button");
 setText(btnDisassociateAccounts, "Disassociate the records");
 btnDisassociateAccounts.title = "Disassociate the records";
 btnDisassociateAccounts.onclick = function () {
  SDK.REST.disassociateRecords(parentAccount.AccountId,
    "Account",
    "Referencedaccount_parent_account",
    childAccount.AccountId,
    function () {
     showLinksToVerifyDisAssociation();
     if (alertFlag.checked == true)
      alert("Disassociation successful.");
    },
    errorHandler);
 }
 writeMessage(btnDisassociateAccounts);
}

function startAssociateAccountToEmail() {
 ///<summary>
 /// Creates an account and email record and displays message to verify that they were created
 /// Then calls associateAccountAsActivityPartyToEmail to associate them
 ///</summary>
 disableElement(btnStartAssociateAccounts);
 disableElement(btnStartAssociateAccountToEmail);

 //Create one account and one email record.
 SDK.REST.createRecord({ Name: "Email Sender Account", Description: "This Account will be the account shown as the sender of an email." },
    "Account",
    function (account) {
     accountRelatedToEmail = account;
     writeMessage("Account Id: {" + accountRelatedToEmail.AccountId + "} Name: \"" + accountRelatedToEmail.Name + "\" created as an account to be the sender of an email.");
     SDK.REST.createRecord({ Subject: "Email Activity", Description: "This email will be shown as sent by the " + accountRelatedToEmail.Name + " record." },
        "Email",
        function (email) {
         emailRecord = email;
         writeMessage("Email ActivityId: {" + emailRecord.ActivityId + "} Subject: \"" + emailRecord.Subject + "\" created to be shown as sent from the " + accountRelatedToEmail.Name + " record.");
         associateAccountAsActivityPartyToEmail();
        },
     errorHandler);
    },
     errorHandler);
 scrollToResults();

}

function associateAccountAsActivityPartyToEmail() {
 ///<summary>
 /// Creates a new ActivityParty record to associate an account record to an email record, making the account the sender of the email.
 /// Shows links to verify the association
 /// Shows buttons to delete the records. It is not possible to disassociate the records by deleting the ActivityParty record using the REST endpoint.
 ///</summary>
 var activityParty = {
  PartyId:
 {
  Id: accountRelatedToEmail.AccountId,
  LogicalName: "account"
 },
  ActivityId: {
   Id: emailRecord.ActivityId,
   LogicalName: "email"
  },
  // Set the participation type (what role the party has on the activity). For this
  // example, we'll put the account in the From field (which has a value of 1).
  // See https://msdn.microsoft.com/en-us/library/gg328549.aspx for other options.
  ParticipationTypeMask: { Value: 1 }
 };

 SDK.REST.createRecord(activityParty,
 "ActivityParty",
 function (ap) {
  test_activityPartyId = ap.ActivityPartyId;
  writeMessage("Created new ActivityParty ActivityPartyId: {" + ap.ActivityPartyId + "}. The account is now related to the email.");
  showLinksToVerifyActivityPartyAssociation();
  showButtonToDeleteAccountAndEmailRecords();
  enableElement(btnResetSample);
  if (alertFlag.checked == true)
   alert("The account is now related to the email.");
 },
 errorHandler);
}

// Note: Attempting to disassociate an activityparty relationship by deleting the ActivityParty record is not allowed.

function showLinksToVerifyActivityPartyAssociation() {
 ///<summary>
 /// Shows links to verify that an account and email are linked
 ///</summary>
 var message = document.createElement("div");
 var message1 = document.createElement("span");
 setText(message1, "Open the ");
 var childRecordlink = document.createElement("a");
 childRecordlink.href = getUrl() + "/main.aspx?etn=email&amp;pagetype=entityrecord&amp;id=%7B" + emailRecord.ActivityId + "%7D";
 childRecordlink.target = "_blank";
 setText(childRecordlink, emailRecord.Subject + " record");
 var message2 = document.createElement("span");
 setText(message2, " to verify that the ");
 var parentRecordlink = document.createElement("a");
 parentRecordlink.href = getUrl() + "/main.aspx?etn=account&amp;pagetype=entityrecord&amp;id=%7B" + accountRelatedToEmail.AccountId + "%7D";
 parentRecordlink.target = "_blank";
 setText(parentRecordlink, accountRelatedToEmail.Name + " record");
 message3 = document.createElement("span");
 setText(message3, " is set as the sender.");
 message.appendChild(message1);
 message.appendChild(childRecordlink);
 message.appendChild(message2);
 message.appendChild(parentRecordlink);
 message.appendChild(message3);
 writeMessage(message);
}

function showButtonToDeleteAccountAndEmailRecords() {
 ///<summary>
 /// Shows button to delete the account and email records created for this sample
 ///</summary>
 var btnDeleteRecords = document.createElement("button");
 setText(btnDeleteRecords, "Delete both records");
 btnDeleteRecords.title = "Delete both records";
 btnDeleteRecords.onclick = function () {
  disableElement(this);
  SDK.REST.deleteRecord(accountRelatedToEmail.AccountId,
            "Account",
            function () { writeMessage(accountRelatedToEmail.Name + " record deleted."); },
            errorHandler);
  SDK.REST.deleteRecord(emailRecord.ActivityId,
            "Email",
            function () {
             writeMessage(emailRecord.Subject + " record deleted.");
             if (alertFlag.checked == true)
              alert("Records deleted.");
            },
            errorHandler);
 }
 writeMessage(btnDeleteRecords);
}

function errorHandler(error) {
 ///<summary>
 /// Displays the message property of errors
 ///</summary>
 writeMessage(error.message);
 if (alertFlag.checked == true)
  alert(error.message);
}

function enableElement(element) {
 ///<summary>
 /// Enables an element that is disabled.
 ///</summary>
 element.removeAttribute("disabled");
}

function disableElement(element) {
 ///<summary>
 /// Disables an element that is enabled.
 ///</summary>
 element.setAttribute("disabled", "disabled");
}

function resetSample() {
 ///<summary>
 /// Clears out the results area and enable buttons to start one of the samples again.
 ///</summary>
 output.innerHTML = "";
 enableElement(btnStartAssociateAccounts);
 enableElement(btnStartAssociateAccountToEmail)
 disableElement(btnResetSample);
 if (alertFlag.checked == true)
  alert("Reset complete.");
}

//Helper function to write data to this page:
function writeMessage(message) {
 ///<summary>
 /// Displays a message or appends an element to the results area.
 ///</summary>
 var li = document.createElement("li");
 if (typeof (message) == "string") {

  setText(li, message);

 }
 else {
  li.appendChild(message);
 }

 output.appendChild(li);
}

function scrollToResults() {
 ///<summary>
 /// Scrolls to the bottom of the page so the results area can be seen.
 ///</summary>
 window.scrollTo(0, document.body.scrollHeight);
}

function setText(node, text) {
 ///<summary>
 /// Used to set the text content of elements to manage differences between browsers.
 ///</summary>
 if (typeof (node.innerText) != "undefined") {
  node.innerText = text;
 }
 else {
  node.textContent = text;
 }
}

sample_/Scripts/SDK.REST.js

此 JavaScript 程式庫提供函數,使用 REST 端點包括 SDK.REST.associateRecordsSDK.REST.disassociateRecords,執行動作。 如需此範例程式庫支援其他作業的資訊,請參閱 範例:使用 OData 端點和 JavaScript 建立、擷取、更新及刪除


if (typeof (SDK) == "undefined")
{ SDK = { __namespace: true }; }
SDK.REST = {
 _context: function () {
  ///<summary>
  /// Private function to the context object.
  ///</summary>
  ///<returns>Context</returns>
  if (typeof GetGlobalContext != "undefined")
  { return GetGlobalContext(); }
  else {
   if (typeof Xrm != "undefined") {
    return Xrm.Page.context;
   }
   else
   { throw new Error("Context is not available."); }
  }
 },
 _getClientUrl: function () {
  ///<summary>
  /// Private function to return the server URL from the context
  ///</summary>
  ///<returns>String</returns>
  var clientUrl = this._context().getClientUrl()

  return clientUrl;
 },
 _ODataPath: function () {
  ///<summary>
  /// Private function to return the path to the REST endpoint.
  ///</summary>
  ///<returns>String</returns>
  return this._getClientUrl() + "/XRMServices/2011/OrganizationData.svc/";
 },
 _errorHandler: function (req) {
  ///<summary>
  /// Private function return an Error object to the errorCallback
  ///</summary>
  ///<param name="req" type="XMLHttpRequest">
  /// The XMLHttpRequest response that returned an error.
  ///</param>
  ///<returns>Error</returns>
  //Error descriptions come from https://support.microsoft.com/kb/193625
  if (req.status == 12029)
  { return new Error("The attempt to connect to the server failed."); }
  if (req.status == 12007)
  { return new Error("The server name could not be resolved."); }
  var errorText;
  try
        { errorText = JSON.parse(req.responseText).error.message.value; }
  catch (e)
        { errorText = req.responseText }

  return new Error("Error : " +
        req.status + ": " +
        req.statusText + ": " + errorText);
 },
 _dateReviver: function (key, value) {
  ///<summary>
  /// Private function to convert matching string values to Date objects.
  ///</summary>
  ///<param name="key" type="String">
  /// The key used to identify the object property
  ///</param>
  ///<param name="value" type="String">
  /// The string value representing a date
  ///</param>
  var a;
  if (typeof value === 'string') {
   a = /Date\(([-+]?\d+)\)/.exec(value);
   if (a) {
    return new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
   }
  }
  return value;
 },
 _parameterCheck: function (parameter, message) {
  ///<summary>
  /// Private function used to check whether required parameters are null or undefined
  ///</summary>
  ///<param name="parameter" type="Object">
  /// The parameter to check;
  ///</param>
  ///<param name="message" type="String">
  /// The error message text to include when the error is thrown.
  ///</param>
  if ((typeof parameter === "undefined") || parameter === null) {
   throw new Error(message);
  }
 },
 _stringParameterCheck: function (parameter, message) {
  ///<summary>
  /// Private function used to check whether required parameters are null or undefined
  ///</summary>
  ///<param name="parameter" type="String">
  /// The string parameter to check;
  ///</param>
  ///<param name="message" type="String">
  /// The error message text to include when the error is thrown.
  ///</param>
  if (typeof parameter != "string") {
   throw new Error(message);
  }
 },
 _callbackParameterCheck: function (callbackParameter, message) {
  ///<summary>
  /// Private function used to check whether required callback parameters are functions
  ///</summary>
  ///<param name="callbackParameter" type="Function">
  /// The callback parameter to check;
  ///</param>
  ///<param name="message" type="String">
  /// The error message text to include when the error is thrown.
  ///</param>
  if (typeof callbackParameter != "function") {
   throw new Error(message);
  }
 },
 createRecord: function (object, type, successCallback, errorCallback) {
  ///<summary>
  /// Sends an asynchronous request to create a new record.
  ///</summary>
  ///<param name="object" type="Object">
  /// A JavaScript object with properties corresponding to the Schema name of
  /// entity attributes that are valid for create operations.
  ///</param>
  ///<param name="type" type="String">
  /// The Schema Name of the Entity type record to create.
  /// For an Account record, use "Account"
  ///</param>
  ///<param name="successCallback" type="Function">
  /// The function that will be passed through and be called by a successful response. 
  /// This function can accept the returned record as a parameter.
  /// </param>
  ///<param name="errorCallback" type="Function">
  /// The function that will be passed through and be called by a failed response. 
  /// This function must accept an Error object as a parameter.
  /// </param>
  this._parameterCheck(object, "SDK.REST.createRecord requires the object parameter.");
  this._stringParameterCheck(type, "SDK.REST.createRecord requires the type parameter is a string.");
  this._callbackParameterCheck(successCallback, "SDK.REST.createRecord requires the successCallback is a function.");
  this._callbackParameterCheck(errorCallback, "SDK.REST.createRecord requires the errorCallback is a function.");
  var req = new XMLHttpRequest();
  req.open("POST", encodeURI(this._ODataPath() + type + "Set"), true);
  req.setRequestHeader("Accept", "application/json");
  req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  req.onreadystatechange = function () {
   if (this.readyState == 4 /* complete */) {
    req.onreadystatechange = null;
    if (this.status == 201) {
     successCallback(JSON.parse(this.responseText, SDK.REST._dateReviver).d);
    }
    else {
     errorCallback(SDK.REST._errorHandler(this));
    }
   }
  };
  req.send(JSON.stringify(object));
 },
 retrieveRecord: function (id, type, select, expand, successCallback, errorCallback) {
  ///<summary>
  /// Sends an asynchronous request to retrieve a record.
  ///</summary>
  ///<param name="id" type="String">
  /// A String representing the GUID value for the record to retrieve.
  ///</param>
  ///<param name="type" type="String">
  /// The Schema Name of the Entity type record to retrieve.
  /// For an Account record, use "Account"
  ///</param>
  ///<param name="select" type="String">
  /// A String representing the $select OData System Query Option to control which
  /// attributes will be returned. This is a comma separated list of Attribute names that are valid for retrieve.
  /// If null all properties for the record will be returned
  ///</param>
  ///<param name="expand" type="String">
  /// A String representing the $expand OData System Query Option value to control which
  /// related records are also returned. This is a comma separated list of of up to 6 entity relationship names
  /// If null no expanded related records will be returned.
  ///</param>
  ///<param name="successCallback" type="Function">
  /// The function that will be passed through and be called by a successful response. 
  /// This function must accept the returned record as a parameter.
  /// </param>
  ///<param name="errorCallback" type="Function">
  /// The function that will be passed through and be called by a failed response. 
  /// This function must accept an Error object as a parameter.
  /// </param>
  this._stringParameterCheck(id, "SDK.REST.retrieveRecord requires the id parameter is a string.");
  this._stringParameterCheck(type, "SDK.REST.retrieveRecord requires the type parameter is a string.");
  if (select != null)
   this._stringParameterCheck(select, "SDK.REST.retrieveRecord requires the select parameter is a string.");
  if (expand != null)
   this._stringParameterCheck(expand, "SDK.REST.retrieveRecord requires the expand parameter is a string.");
  this._callbackParameterCheck(successCallback, "SDK.REST.retrieveRecord requires the successCallback parameter is a function.");
  this._callbackParameterCheck(errorCallback, "SDK.REST.retrieveRecord requires the errorCallback parameter is a function.");

  var systemQueryOptions = "";

  if (select != null || expand != null) {
   systemQueryOptions = "?";
   if (select != null) {
    var selectString = "$select=" + select;
    if (expand != null) {
     selectString = selectString + "," + expand;
    }
    systemQueryOptions = systemQueryOptions + selectString;
   }
   if (expand != null) {
    systemQueryOptions = systemQueryOptions + "&amp;$expand=" + expand;
   }
  }


  var req = new XMLHttpRequest();
  req.open("GET", encodeURI(this._ODataPath() + type + "Set(guid'" + id + "')" + systemQueryOptions), true);
  req.setRequestHeader("Accept", "application/json");
  req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  req.onreadystatechange = function () {
   if (this.readyState == 4 /* complete */) {
    req.onreadystatechange = null;
    if (this.status == 200) {
     successCallback(JSON.parse(this.responseText, SDK.REST._dateReviver).d);
    }
    else {
     errorCallback(SDK.REST._errorHandler(this));
    }
   }
  };
  req.send();
 },
 updateRecord: function (id, object, type, successCallback, errorCallback) {
  ///<summary>
  /// Sends an asynchronous request to update a record.
  ///</summary>
  ///<param name="id" type="String">
  /// A String representing the GUID value for the record to retrieve.
  ///</param>
  ///<param name="object" type="Object">
  /// A JavaScript object with properties corresponding to the Schema Names for
  /// entity attributes that are valid for update operations.
  ///</param>
  ///<param name="type" type="String">
  /// The Schema Name of the Entity type record to retrieve.
  /// For an Account record, use "Account"
  ///</param>
  ///<param name="successCallback" type="Function">
  /// The function that will be passed through and be called by a successful response. 
  /// Nothing will be returned to this function.
  /// </param>
  ///<param name="errorCallback" type="Function">
  /// The function that will be passed through and be called by a failed response. 
  /// This function must accept an Error object as a parameter.
  /// </param>
  this._stringParameterCheck(id, "SDK.REST.updateRecord requires the id parameter.");
  this._parameterCheck(object, "SDK.REST.updateRecord requires the object parameter.");
  this._stringParameterCheck(type, "SDK.REST.updateRecord requires the type parameter.");
  this._callbackParameterCheck(successCallback, "SDK.REST.updateRecord requires the successCallback is a function.");
  this._callbackParameterCheck(errorCallback, "SDK.REST.updateRecord requires the errorCallback is a function.");
  var req = new XMLHttpRequest();

  req.open("POST", encodeURI(this._ODataPath() + type + "Set(guid'" + id + "')"), true);
  req.setRequestHeader("Accept", "application/json");
  req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  req.setRequestHeader("X-HTTP-Method", "MERGE");
  req.onreadystatechange = function () {
   if (this.readyState == 4 /* complete */) {
    req.onreadystatechange = null;
    if (this.status == 204 || this.status == 1223) {
     successCallback();
    }
    else {
     errorCallback(SDK.REST._errorHandler(this));
    }
   }
  };
  req.send(JSON.stringify(object));
 },
 deleteRecord: function (id, type, successCallback, errorCallback) {
  ///<summary>
  /// Sends an asynchronous request to delete a record.
  ///</summary>
  ///<param name="id" type="String">
  /// A String representing the GUID value for the record to delete.
  ///</param>
  ///<param name="type" type="String">
  /// The Schema Name of the Entity type record to delete.
  /// For an Account record, use "Account"
  ///</param>
  ///<param name="successCallback" type="Function">
  /// The function that will be passed through and be called by a successful response. 
  /// Nothing will be returned to this function.
  /// </param>
  ///<param name="errorCallback" type="Function">
  /// The function that will be passed through and be called by a failed response. 
  /// This function must accept an Error object as a parameter.
  /// </param>
  this._stringParameterCheck(id, "SDK.REST.deleteRecord requires the id parameter.");
  this._stringParameterCheck(type, "SDK.REST.deleteRecord requires the type parameter.");
  this._callbackParameterCheck(successCallback, "SDK.REST.deleteRecord requires the successCallback is a function.");
  this._callbackParameterCheck(errorCallback, "SDK.REST.deleteRecord requires the errorCallback is a function.");
  var req = new XMLHttpRequest();
  req.open("POST", encodeURI(this._ODataPath() + type + "Set(guid'" + id + "')"), true);
  req.setRequestHeader("Accept", "application/json");
  req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  req.setRequestHeader("X-HTTP-Method", "DELETE");
  req.onreadystatechange = function () {

   if (this.readyState == 4 /* complete */) {
    req.onreadystatechange = null;
    if (this.status == 204 || this.status == 1223) {
     successCallback();
    }
    else {
     errorCallback(SDK.REST._errorHandler(this));
    }
   }
  };
  req.send();

 },
 retrieveMultipleRecords: function (type, options, successCallback, errorCallback, OnComplete) {
  ///<summary>
  /// Sends an asynchronous request to retrieve records.
  ///</summary>
  ///<param name="type" type="String">
  /// The Schema Name of the Entity type record to retrieve.
  /// For an Account record, use "Account"
  ///</param>
  ///<param name="options" type="String">
  /// A String representing the OData System Query Options to control the data returned
  ///</param>
  ///<param name="successCallback" type="Function">
  /// The function that will be passed through and be called for each page of records returned.
  /// Each page is 50 records. If you expect that more than one page of records will be returned,
  /// this function should loop through the results and push the records into an array outside of the function.
  /// Use the OnComplete event handler to know when all the records have been processed.
  /// </param>
  ///<param name="errorCallback" type="Function">
  /// The function that will be passed through and be called by a failed response. 
  /// This function must accept an Error object as a parameter.
  /// </param>
  ///<param name="OnComplete" type="Function">
  /// The function that will be called when all the requested records have been returned.
  /// No parameters are passed to this function.
  /// </param>
  this._stringParameterCheck(type, "SDK.REST.retrieveMultipleRecords requires the type parameter is a string.");
  if (options != null)
   this._stringParameterCheck(options, "SDK.REST.retrieveMultipleRecords requires the options parameter is a string.");
  this._callbackParameterCheck(successCallback, "SDK.REST.retrieveMultipleRecords requires the successCallback parameter is a function.");
  this._callbackParameterCheck(errorCallback, "SDK.REST.retrieveMultipleRecords requires the errorCallback parameter is a function.");
  this._callbackParameterCheck(OnComplete, "SDK.REST.retrieveMultipleRecords requires the OnComplete parameter is a function.");

  var optionsString;
  if (options != null) {
   if (options.charAt(0) != "?") {
    optionsString = "?" + options;
   }
   else
   { optionsString = options; }
  }
  var req = new XMLHttpRequest();
  req.open("GET", this._ODataPath() + type + "Set" + optionsString, true);
  req.setRequestHeader("Accept", "application/json");
  req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  req.onreadystatechange = function () {
   if (this.readyState == 4 /* complete */) {
    req.onreadystatechange = null;
    if (this.status == 200) {
     var returned = JSON.parse(this.responseText, SDK.REST._dateReviver).d;
     successCallback(returned.results);
     if (returned.__next != null) {
      var queryOptions = returned.__next.substring((SDK.REST._ODataPath() + type + "Set").length);
      SDK.REST.retrieveMultipleRecords(type, queryOptions, successCallback, errorCallback, OnComplete);
     }
     else
     { OnComplete(); }
    }
    else {
     errorCallback(SDK.REST._errorHandler(this));
    }
   }
  };
  req.send();
 },
 associateRecords: function (parentId, parentType, relationshipName, childId, childType, successCallback, errorCallback) {
  this._stringParameterCheck(parentId, "SDK.REST.associateRecords requires the parentId parameter is a string.");
  ///<param name="parentId" type="String">
  /// The Id of the record to be the parent record in the relationship
  /// </param>
  ///<param name="parentType" type="String">
  /// The Schema Name of the Entity type for the parent record.
  /// For an Account record, use "Account"
  /// </param>
  ///<param name="relationshipName" type="String">
  /// The Schema Name of the Entity Relationship to use to associate the records.
  /// To associate account records as a Parent account, use "Referencedaccount_parent_account"
  /// </param>
  ///<param name="childId" type="String">
  /// The Id of the record to be the child record in the relationship
  /// </param>
  ///<param name="childType" type="String">
  /// The Schema Name of the Entity type for the child record.
  /// For an Account record, use "Account"
  /// </param>
  ///<param name="successCallback" type="Function">
  /// The function that will be passed through and be called by a successful response. 
  /// Nothing will be returned to this function.
  /// </param>
  ///<param name="errorCallback" type="Function">
  /// The function that will be passed through and be called by a failed response. 
  /// This function must accept an Error object as a parameter.
  /// </param>
  this._stringParameterCheck(parentType, "SDK.REST.associateRecords requires the parentType parameter is a string.");
  this._stringParameterCheck(relationshipName, "SDK.REST.associateRecords requires the relationshipName parameter is a string.");
  this._stringParameterCheck(childId, "SDK.REST.associateRecords requires the childId parameter is a string.");
  this._stringParameterCheck(childType, "SDK.REST.associateRecords requires the childType parameter is a string.");
  this._callbackParameterCheck(successCallback, "SDK.REST.associateRecords requires the successCallback parameter is a function.");
  this._callbackParameterCheck(errorCallback, "SDK.REST.associateRecords requires the errorCallback parameter is a function.");

  var req = new XMLHttpRequest();
  req.open("POST", encodeURI(this._ODataPath() + parentType + "Set(guid'" + parentId + "')/$links/" + relationshipName), true);
  req.setRequestHeader("Accept", "application/json");
  req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  req.onreadystatechange = function () {
   if (this.readyState == 4 /* complete */) {
    req.onreadystatechange = null;
    if (this.status == 204 || this.status == 1223) {
     successCallback();
    }
    else {
     errorCallback(SDK.REST._errorHandler(this));
    }
   }
  };
  var childEntityReference = {}
  childEntityReference.uri = this._ODataPath() + "/" + childType + "Set(guid'" + childId + "')";
  req.send(JSON.stringify(childEntityReference));
 },
 disassociateRecords: function (parentId, parentType, relationshipName, childId, successCallback, errorCallback) {
  this._stringParameterCheck(parentId, "SDK.REST.disassociateRecords requires the parentId parameter is a string.");
  ///<param name="parentId" type="String">
  /// The Id of the record to be the parent record in the relationship
  /// </param>
  ///<param name="parentType" type="String">
  /// The Schema Name of the Entity type for the parent record.
  /// For an Account record, use "Account"
  /// </param>
  ///<param name="relationshipName" type="String">
  /// The Schema Name of the Entity Relationship to use to disassociate the records.
  /// To disassociate account records as a Parent account, use "Referencedaccount_parent_account"
  /// </param>
  ///<param name="childId" type="String">
  /// The Id of the record to be disassociated as the child record in the relationship
  /// </param>
  ///<param name="successCallback" type="Function">
  /// The function that will be passed through and be called by a successful response. 
  /// Nothing will be returned to this function.
  /// </param>
  ///<param name="errorCallback" type="Function">
  /// The function that will be passed through and be called by a failed response. 
  /// This function must accept an Error object as a parameter.
  /// </param>
  this._stringParameterCheck(parentType, "SDK.REST.disassociateRecords requires the parentType parameter is a string.");
  this._stringParameterCheck(relationshipName, "SDK.REST.disassociateRecords requires the relationshipName parameter is a string.");
  this._stringParameterCheck(childId, "SDK.REST.disassociateRecords requires the childId parameter is a string.");
  this._callbackParameterCheck(successCallback, "SDK.REST.disassociateRecords requires the successCallback parameter is a function.");
  this._callbackParameterCheck(errorCallback, "SDK.REST.disassociateRecords requires the errorCallback parameter is a function.");

  var req = new XMLHttpRequest();
  req.open("POST", encodeURI(this._ODataPath() + parentType + "Set(guid'" + parentId + "')/$links/" + relationshipName + "(guid'" + childId + "')"), true);
  req.setRequestHeader("Accept", "application/json");
  req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  req.setRequestHeader("X-HTTP-Method", "DELETE");
  req.onreadystatechange = function () {
   if (this.readyState == 4 /* complete */) {
    req.onreadystatechange = null;
    if (this.status == 204 || this.status == 1223) {
     successCallback();
    }
    else {
     errorCallback(SDK.REST._errorHandler(this));
    }
   }
  };
  req.send();
 },
 __namespace: true
};

這些函數使用 URL 的 $links 區段以及特定導覽屬性 (傳遞至函數的 relationshipName 參數),建立或刪除關聯。 如需詳細資訊,請參閱 OData:URI 慣例 3.2. 項目之間的定址連結

另請參閱

使用 OData 端點搭配 Web 資源
相關實體
範例:OData 端點 JQuery 連絡人編輯器
範例:使用 OData 端點和 JavaScript 建立、擷取、更新及刪除
MSDN:使用選項組選項與 REST 端點 - JScript

© 2017 Microsoft. 著作權所有,並保留一切權利。 著作權