サンプル: OData エンドポイントと JavaScript および jQuery を使用した作成、取得、更新、および削除

 

公開日: 2016年11月

対象: Dynamics CRM 2015

このサンプル コードは、Microsoft Dynamics CRM 2015 および Microsoft Dynamics CRM Online 2015 更新プログラム 向けです。Microsoft Dynamics CRM SDK パッケージをダウンロードします。 このサンプル コードは、ダウンロード パッケージの次の場所にあります。

SampleCode\JS\RESTEndpoint\JQueryRESTDataOperations

重要

フォーム スクリプトまたはリボン コマンドで jQuery を使用することはお勧めしません。jQuery に依存関係のない同様の機能を持つサンプル ライブラリについては、「サンプル: OData エンドポイントと JavaScript を使用した作成、取得、更新、および削除」を参照してください。

詳細:jQuery の使用

注意

このサンプルがどのように機能するかを確認するだけであれば、ダウンロード ファイルに含まれている JQueryRESTDataOperations_1_0_1_2_managed.zip マネージド ソリューションをインストール (インポート) します。 これは JQuery REST データ操作 ソリューションをインストールします。 この管理ソリューションをインストールし、以下の名前を使用して Web リソースを作成する場合は、管理ソリューションをアンインストール (削除) しない限り、ソリューション発行者のカスタマイズ接頭辞を "sample" にすることはできません。

要件

このサンプルでは、次の web リソースを使用します。

  • sample_/JQueryRESTDataOperations.htm
    この HTML ページを使用して、サンプルを開始し、アクションの進行状況を表示します。 このページは、JQuery REST データ操作ソリューション用の構成ページとして設定されます。

  • sample_/Scripts/jquery1.9.1.min.js
    Microsoft Visual Studio Web アプリケーション プロジェクトに含まれる jQuery の最小化バージョンです。 このライブラリの内容は、このトピックには含まれていません。

  • sample_/Scripts/JQueryRESTDataOperationsSample.js
    操作を実行する JScript Web リソースです。 このライブラリは、SDK.JQuery.js ライブラリに依存します。

  • sample_/Scripts/SDK.JQuery.js
    JQuery を使用して REST エンドポイントでアクションを実行する、再利用可能ライブラリを含む JScript Web リソースです。

注意

JQuery との依存関係のない同様のライブラリについては、「サンプル: OData エンドポイントと JavaScript を使用した作成、取得、更新、および削除」を参照してください。

注意

カスタマイズの接頭辞 “sample_” はコード内では使用されません。 これらのサンプルは、任意の発行者からのカスタマイズ接頭辞を使用して機能します。 ただし、シミュレートされた Scripts フォルダーの相対パスは、Web リソースの名前に含まれる必要があります。

使用例

このサンプルの機能は次のとおりです:

  • REST エンドポイントを JScript および jQuery と共に使用して、作成、取得、更新、および削除の操作を実行します。

  • jQuery ($.ajax メソッドを使用してデータ操作を実行できる一般的なライブラリ) を使用します。

  • 新しい取引先企業レコードについて、複数の操作を順番に実行します。

    このサンプルを実行する際には、作成されたレコードを削除するかどうかを選択できます。

  • Web リソースをプレビューする際に、次のような結果を表示します。sample_/JQueryRESTDataOperations.htm

    1. 取引先責任者を <システム内の最初の取引先担当者レコードのフルネーム> に設定します。

    2. 優先連絡方法を電子メールに設定します。

    3. 売上高を 2 百万に設定します。

    4. 連絡方法として電話を「許可しない」に設定します。

    5. アカウント ID が "7780cef2-fdf4-e011-9d26-00155dba3819" の "Test Account Name" というアカウントが作成されました。

    6. アカウント ID が 7780cef2-fdf4-e011-9d26-00155dba3819 のアカウントを検索します。

    7. Test Account Name というアカウントが検索されます。 このアカウントは、2011 年 10 月 12 日水曜日 11:13:56 (太平洋夏時間) に作成されました。

    8. アカウント名を Updated Account Name に変更します。

    9. 住所情報を追加します。

    10. 電子メール アドレスを設定します。

    11. アカウント レコードの変更が保存されました。

    12. アカウント レコードの削除を選択しました。

    13. アカウントが削除されました。

アカウントを削除しない場合、作成したレコードをオープンするためのリンクが表示されます。

sample_/JQueryRESTDataOperations.htm

このページには、いくつかの説明と、サンプルを開始およびリセットするボタンがあります。


<html lang="en-us">
<head>
 <title>JQuery REST Data Operations</title>
 <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <script src="Scripts/jquery_1.9.1.min.js"></script>
 <script src="Scripts/SDK.JQuery.js" type="text/javascript"></script>
 <script src="Scripts/JQueryRESTDataOperationsSample.js" type="text/javascript"></script>

<!-- /WebResources/ClientGlobalContext.js.aspx is accessed using a relative path
because the name of the Web Resource created from this file is "sample_/JQueryRESTDataOperationsSample.htm".

The use of the backslash within the name creates a virtual folder that must be considered
in relative links between Web Resources.
-->
 <script type="text/javascript" src="../ClientGlobalContext.js.aspx"></script>

</head>
<body style="background-color: White; font-family:Segoe UI;">
<h1>JavaScript jQuery REST Data Operations Sample</h1>
<p>This page uses the <b>sample_/Scripts/JQueryRESTDataOperationsSample.js</b> JScript library to create, retrieve, and update an
 account record. It also provides the option to delete or view the record.</p>
<p>The <b>sample_/Scripts/JQueryRESTDataOperationsSample.js</b> JScript library uses the <b>sample_/Scripts/SDK.JQuery.js</b> JScript library to perform the data operations.</p>
<p>Use the buttons below to verify the functionality of this sample.</p>
 <button id="start" title="Click this button to start the sample.">
  Start</button>
 <button id="reset" title="Click this button to reset the sample." disabled="disabled">
  Reset</button>
 <ol id="output">
 </ol>
</body>
</html>

sample_/Scripts/JQueryRESTDataOperationsSample.js

このライブラリには、ページ上の UI 要素を管理する関数と、このサンプルで実行するアクションが含まれます。 このライブラリに含まれる関数は、sample_/Scripts/SDK.JQuery.js 内の再利用可能な汎用メソッドに依存します。


/// <reference path="SDK.JQuery.js" />
/// <reference path="jquery-1.9.1.js" />


var primaryContact = null;
var startButton;
var resetButton;
var output; //The <ol> element used by the writeMessage function to display text showing the progress of this sample.


$(function () {
 getFirstContactToBePrimaryContact();
 startButton = $("#start");
 startButton.click(createAccount);
 resetButton = $("#reset");
 resetButton.click(resetSample);
 output = $("#output");

});


function createAccount() {
 startButton.attr("disabled", "disabled");
 var account = {};
 account.Name = "Test Account Name";
 account.Description = "This account was created by the JQueryRESTDataOperations sample.";
 if (primaryContact != null) {
  //Set a lookup value
  writeMessage("Setting the primary contact to: " + primaryContact.FullName + ".");
  account.PrimaryContactId = { Id: primaryContact.ContactId, LogicalName: "contact", Name: primaryContact.FullName };

 }
 //Set a picklist value
 writeMessage("Setting Preferred Contact Method to E-mail.");
 account.PreferredContactMethodCode = { Value: 2 }; //E-mail

 //Set a money value
 writeMessage("Setting Annual Revenue to Two Million .");
 account.Revenue = { Value: "2000000.00" }; //Set Annual Revenue

 //Set a Boolean value
 writeMessage("Setting Contact Method Phone to \"Do Not Allow\".");
 account.DoNotPhone = true; //Do Not Allow

 //Add Two Tasks
 var today = new Date();
 var startDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 3); //Set a date three days in the future.

 var LowPriTask = { Subject: "Low Priority Task", ScheduledStart: startDate, PriorityCode: { Value: 0} }; //Low Priority Task
 var HighPriTask = { Subject: "High Priority Task", ScheduledStart: startDate, PriorityCode: { Value: 2} }; //High Priority Task
 account.Account_Tasks = [LowPriTask, HighPriTask]



 //Create the Account
 SDK.JQuery.createRecord(
     account,
     "Account",
     function (account) {
      writeMessage("The account named \"" + account.Name + "\" was created with the AccountId : \"" + account.AccountId + "\".");
      writeMessage("Retrieving account with the AccountId: \"" + account.AccountId + "\".");
      retrieveAccount(account.AccountId)
     },
     errorHandler
   );

}

function retrieveAccount(AccountId) {
 SDK.JQuery.retrieveRecord(
     AccountId,
     "Account",
     null, null,
     function (account) {
      writeMessage("Retrieved the account named \"" + account.Name + "\". This account was created on : \"" + account.CreatedOn + "\".");
      updateAccount(AccountId);
     },
     errorHandler
   );
}

function updateAccount(AccountId) {
 var account = {};
 writeMessage("Changing the account Name to \"Updated Account Name\".");
 account.Name = "Updated Account Name";
 writeMessage("Adding Address information");
 account.Address1_AddressTypeCode = { Value: 3 }; //Address 1: Address Type = Primary
 account.Address1_City = "Sammamish";
 account.Address1_Line1 = "123 Maple St.";
 account.Address1_PostalCode = "98074";
 account.Address1_StateOrProvince = "WA";
 writeMessage("Setting E-Mail address");
 account.EMailAddress1 = "someone@microsoft.com";


 SDK.JQuery.updateRecord(
     AccountId,
     account,
     "Account",
     function () {
      writeMessage("The account record changes were saved");
      deleteAccount(AccountId);
     },
     errorHandler
   );
}

function deleteAccount(AccountId) {
 if (confirm("Do you want to delete this account record?")) {
  writeMessage("You chose to delete the account record.");
  SDK.JQuery.deleteRecord(
       AccountId,
       "Account",
       function () {
        writeMessage("The account was deleted.");
        enableResetButton();
       },
       errorHandler
     );
 }
 else {
  var urlToAccountRecord = SDK.JQuery._getClientUrl() + "/main.aspx?etc=1&amp;id=%7b" + AccountId + "%7d&amp;pagetype=entityrecord";
  $("<li><span>You chose not to delete the record. You can view the record </span><a href='" +
  urlToAccountRecord + 
  "' target='_blank'>here</a></li>").appendTo(output);
  enableResetButton();
 }
}

function getFirstContactToBePrimaryContact() {

 SDK.JQuery.retrieveMultipleRecords(
     "Contact",
     "$select=ContactId,FullName&amp;$top=1",
     function (results) {
      var firstResult = results[0];
      if (firstResult != null) {
       primaryContact = results[0];
      }
      else {
       writeMessage("No Contact records are available to set as the primary contact for the account.");
      }
     },
     errorHandler,
     function () {
      //OnComplete handler
     }
   );
}

function errorHandler(error) {
 writeMessage(error.message);
}

function enableResetButton() {
 resetButton.removeAttr("disabled");
}

function resetSample() {
 output.empty();
 startButton.removeAttr("disabled");
 resetButton.attr("disabled", "disabled");
}

//Helper function to write data to this page:
function writeMessage(message) {
 $("<li>" + message + "</li>").appendTo(output);
}

sample_/Scripts/SDK.JQuery.js

このライブラリには、次の操作用のパブリック メソッドが含まれます。

  • createRecord

  • retrieveRecord

  • updateRecord

  • deleteRecord

  • retrieveMultipleRecords

これらの各メソッドには、successCallback および errorCallback パラメーターがあります。 これらのパラメーターは、データ操作の成功または失敗時に呼び出される関数への参照を受け取ります。


/// <reference path="jquery1.4.1vsdoc.js" />

if (typeof (SDK) == "undefined")
{ SDK = { __namespace: true }; }
SDK.JQuery = {
 _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 serverUrl = this._context().getClientUrl()

  return serverUrl;
 },
 _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>
  return new Error("Error : " +
        req.status + ": " +
        req.statusText + ": " +
        JSON.parse(req.responseText).error.message.value);
 },
 _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>
  this._parameterCheck(object, "SDK.JQuery.createRecord requires the object parameter.");
  ///<param name="type" type="String">
  /// The Schema Name of the Entity type record to create.
  /// For an Account record, use "Account"
  ///</param>
  this._stringParameterCheck(type, "SDK.JQuery.createRecord requires the type parameter is a string.");
  ///<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>
  this._callbackParameterCheck(successCallback, "SDK.JQuery.createRecord requires the successCallback is a function.");
  ///<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._callbackParameterCheck(errorCallback, "SDK.JQuery.createRecord requires the errorCallback is a function.");

  var jsonEntity = window.JSON.stringify(object);

  $.ajax({ type: "POST",
   contentType: "application/json; charset=utf-8",
   datatype: "json",
   url: this._ODataPath() + type + "Set",
   data: jsonEntity,
   beforeSend: function (xhr) {
    //Specifying this header ensures that the results will be returned as JSON.             
    xhr.setRequestHeader("Accept", "application/json");
   },
   success: function (data, textStatus, xhr) {
    successCallback(data.d);
   },
   error: function (xhr, textStatus, errorThrown) {
    errorCallback(SDK.JQuery._errorHandler(xhr));
   }
  });
 },
 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>
  this._stringParameterCheck(id, "SDK.JQuery.retrieveRecord requires the id parameter is a string.");
  ///<param name="type" type="String">
  /// The Schema Name of the Entity type record to retrieve.
  /// For an Account record, use "Account"
  ///</param>
  this._stringParameterCheck(type, "SDK.JQuery.retrieveRecord requires the type parameter is a string.");
  ///<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>
  if (select != null)
   this._stringParameterCheck(select, "SDK.JQuery.retrieveRecord requires the select parameter is a string.");
  ///<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>
  if (expand != null)
   this._stringParameterCheck(expand, "SDK.JQuery.retrieveRecord requires the expand parameter is a string.");
  ///<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>
  this._callbackParameterCheck(successCallback, "SDK.JQuery.retrieveRecord requires the successCallback parameter is a function.");
  ///<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._callbackParameterCheck(errorCallback, "SDK.JQuery.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;
   }
  }

  $.ajax({
   type: "GET",
   contentType: "application/json; charset=utf-8",
   datatype: "json",
   url: this._ODataPath() + type + "Set" + "(guid'" + id + "')" + systemQueryOptions,
   beforeSend: function (xhr) {
    //Specifying this header ensures that the results will be returned as JSON.             
    xhr.setRequestHeader("Accept", "application/json");
   },
   success: function (data, textStatus, xhr) {
    //JQuery does not provide an opportunity to specify a date reviver so this code
   // parses the xhr.responseText rather than use the data parameter passed by JQuery.
    successCallback(JSON.parse(xhr.responseText, SDK.JQuery._dateReviver).d);
   },
   error: function (xhr, textStatus, errorThrown) {
    errorCallback(SDK.JQuery._errorHandler(xhr));
   }
  });
 },
 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>
  this._stringParameterCheck(id, "SDK.JQuery.updateRecord requires the id parameter.");
  ///<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>
  this._parameterCheck(object, "SDK.JQuery.updateRecord requires the object parameter.");
  ///<param name="type" type="String">
  /// The Schema Name of the Entity type record to retrieve.
  /// For an Account record, use "Account"
  ///</param>
  this._stringParameterCheck(type, "SDK.JQuery.updateRecord requires the type parameter.");
  ///<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>
  this._callbackParameterCheck(successCallback, "SDK.JQuery.updateRecord requires the successCallback is a function.");
  ///<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._callbackParameterCheck(errorCallback, "SDK.JQuery.updateRecord requires the errorCallback is a function.");

  var jsonEntity = window.JSON.stringify(object);

  $.ajax({
   type: "POST",
   contentType: "application/json; charset=utf-8",
   datatype: "json",
   data: jsonEntity,
   url: this._ODataPath() + type + "Set" + "(guid'" + id + "')",
   beforeSend: function (xhr) {
    //Specifying this header ensures that the results will be returned as JSON.             
    xhr.setRequestHeader("Accept", "application/json");
    //Specify the HTTP method MERGE to update just the changes you are submitting.             
    xhr.setRequestHeader("X-HTTP-Method", "MERGE");
   },
   success: function (data, textStatus, xhr) {
   //Nothing is returned to the success function
    successCallback();
   },
   error: function (xhr, textStatus, errorThrown) {
    errorCallback(SDK.JQuery._errorHandler(xhr));
   }
  });
 },
 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>
  this._stringParameterCheck(id, "SDK.JQuery.deleteRecord requires the id parameter.");
  ///<param name="type" type="String">
  /// The Schema Name of the Entity type record to delete.
  /// For an Account record, use "Account"
  ///</param>
  this._stringParameterCheck(type, "SDK.JQuery.deleteRecord requires the type parameter.");
  ///<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>
  this._callbackParameterCheck(successCallback, "SDK.JQuery.deleteRecord requires the successCallback is a function.");
  ///<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._callbackParameterCheck(errorCallback, "SDK.JQuery.deleteRecord requires the errorCallback is a function.");

  $.ajax({
   type: "POST",
   contentType: "application/json; charset=utf-8",
   datatype: "json",
   url: this._ODataPath() + type + "Set(guid'" + id + "')",
   beforeSend: function (XMLHttpRequest) {
    //Specifying this header ensures that the results will be returned as JSON.                 
    XMLHttpRequest.setRequestHeader("Accept", "application/json");
    //Specify the HTTP method DELETE to perform a delete operation.                 
    XMLHttpRequest.setRequestHeader("X-HTTP-Method", "DELETE");
   },
   success: function (data, textStatus, xhr) {
   // Nothing is returned to the success function.
    successCallback();
   },
   error: function (xhr, textStatus, errorThrown) {
    errorCallback(SDK.JQuery._errorHandler(xhr));
   }
  });
 },
 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 records to retrieve
  /// For an Account record, use "Account"
  ///</param>
  this._stringParameterCheck(type, "SDK.JQuery.retrieveMultipleRecords requires the type parameter is a string.");
  ///<param name="options" type="String">
  /// A String representing the OData System Query Options to control the data returned
  /// Do not include the $top option, use the top parameters to set the maximum number of records to return.
  ///</param>
  if (options != null)
   this._stringParameterCheck(options, "SDK.JQuery.retrieveMultipleRecords requires the options parameter is a string.");
  ///<param name="successCallback" type="Function">
  /// The function that will be passed through and be called for each page of records returned.
  /// This function should loop through the results and push the records into an array.
  /// </param>
  this._callbackParameterCheck(successCallback, "SDK.JQuery.retrieveMultipleRecords requires the successCallback parameter is a function.");
  ///<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._callbackParameterCheck(errorCallback, "SDK.JQuery.retrieveMultipleRecords requires the errorCallback parameter is a function.");
  ///<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._callbackParameterCheck(OnComplete, "SDK.JQuery.retrieveMultipleRecords requires the OnComplete parameter is a function.");

  var optionsString;
  if (options != null) {
   if (options.charAt(0) != "?") {
    optionsString = "?" + options;
   }
   else
   { optionsString = options; }
  }

  $.ajax({
   type: "GET",
   contentType: "application/json; charset=utf-8",
   datatype: "json",
   url: this._ODataPath() + type + "Set" + optionsString,
   beforeSend: function (XMLHttpRequest) {
    //Specifying this header ensures that the results will be returned as JSON.             
    XMLHttpRequest.setRequestHeader("Accept", "application/json");
   },
   success: function (data, textStatus, xhr) {
    if (data &amp;&amp; data.d &amp;&amp; data.d.results) {
     successCallback(JSON.parse(xhr.responseText, SDK.JQuery._dateReviver).d.results);
     if (data.d.__next != null) {
      var queryOptions = data.d.__next.substring((SDK.JQuery._ODataPath() + type + "Set").length);
      SDK.JQuery.retrieveMultipleRecords(type, queryOptions, successCallback, errorCallback, OnComplete);
     }
     else
     { OnComplete(); }
    }
   },
   error: function (xhr, textStatus, errorThrown) {
    errorCallback(SDK.JQuery._errorHandler(xhr));
   }
  });
 },
 __namespace: true
};

関連項目

JQuery
Web リソースで OData エンドポイントを使用する
サンプル: OData エンドポイントと JavaScript を使用した作成、取得、更新、および削除
サンプル: OData エンドポイントと JavaScript を使用した複数のレコードの取得
技術記事: Using Option Set Options with the REST Endpoint - JScript (REST エンドポイントでのオプション セットのオプションの使用 - JScript)

© 2017 Microsoft. All rights reserved. 著作権