使用入口網站 Web API 寫入、更新和刪除作業

您可以執行入口網站中可用的 WEB API 作業。 Web API 作業是由 HTTP 要求和回應所組成。 本文提供可在 HTTP 要求中使用的範例寫入、更新和刪除作業、方法、URI 及範例 JSON。

重要

  • 您的入口網站版本必須為 9.3.3.x 或更新版本,才能使本功能運作。

先決條件

  • 為 Web API 作業啟用資料表和欄位。 其他資訊:WEB API 的網站設定

  • 入口網站 Web API 會存取資料表記錄,並遵循透過關聯的 Web 角色授與使用者的資料表權限。 請確定您已設定正確的資料表權限。 其他資訊:建立 Web 角色

在資料表中建立記錄

注意

使用入口網站 API 來參考 Dataverse 資料表時,需要使用 EntitySetName,例如若要存取 account 資料表,程式碼語法將使用 accounts 的 EntitySetName。

基本建立

選用 方法 URI JSON 範例
基本建立 張貼 [入口網站 URI]/_api/accounts {"名稱":"範例客戶"}

例如,發佈到客戶資料表集的以下要求本文,將在建立帳戶的內容中,建立共四個新資料表 (包括客戶)。

  • 之所以建立連絡人是因為該連絡人被定義為單值瀏覽屬性 primarycontactid 的物件屬性。
  • 之所以建立商機是因為它被定義為陣列中的物件,其設定為集合值瀏覽屬性 opportunity_customer_accounts 的值。
  • 之所以建立工作是因為它被定義為陣列中的物件,其設定為集合值瀏覽屬性 Opportunity_Tasks 的值。
{
 "name": "Sample Account",
 "primarycontactid":
 {
     "firstname": "Alton",
     "lastname": "Stott"
 },
 "opportunity_customer_accounts":
 [
  {
      "name": "Opportunity associated to Sample Account",
      "Opportunity_Tasks":
      [
       { "subject": "Task associated to opportunity" }
      ]
  }
 ]
}

在建立時建立資料表記錄

Operation 方法 URI JSON 範例
在建立時建立資料表記錄 POST [入口網站 URI]/_api/accounts {"name":"Sample Account","primarycontactid@odata.bind":"/contacts(00000000-0000-0000-0000-000000000001)"}

通過 Web API 建立註釋的範例 JSON

{
    "new_attribute1": "test attribute 1",
    "new_attribute2": "test attribute 2",
    "new_comments": "test comments",
    "new_recordurl": recordURL,
    "new_feedback_Annotations":
        [
            {
                "notetext": "Screenshot attached",
                "subject": "Attachment",
                "filename": file.name,
                "mimetype": file.type,
                "documentbody": base64str,
            }
        ]
    }

documentbody 會包含 base64 字串格式的附件。

使用 Web API 更新和刪除記錄

基本更新

作業 方法 URI JSON 範例
基本更新 PATCH [入口網站 URI]/_api/accounts(00000000-0000-0000-0000-000000000001) { "name": "Updated Sample Account ", "creditonhold": true, "address1_latitude": 47.639583, "description": "This is the updated description of the sample account", "revenue": 6000000, "accountcategorycode": 2 }

更新單一屬性值

作業 方法 URI JSON 範例
更新單一屬性值 PUT [入口網站 URI]/_api/accounts(00000000-0000-0000-0000-000000000001)/name {"value": "Updated Sample Account Name"}

刪除或清除欄位值

選用 方法 URI
刪除或清除欄位值 DELETE [入口網站 URI]/_api/accounts(00000000-0000-0000-0000-000000000001)/description

基本刪除

作業 方法 URI
基本刪除 DELETE [入口網站 URI]/_api/accounts(00000000-0000-0000-0000-000000000001)

使用 Web API 關聯和取消關聯資料表

新增集合值導覽屬性的參考

作業 方法 URI JSON 範例
新增集合值導覽屬性的參考 張貼 [入口網站 URI]/_api/accounts(00000000-0000-0000-0000-000000000002)/opportunity_customer_accounts/$ref {"@odata.id":"[Portal URI]/_api/opportunities(00000000-0000-0000-0000-000000000001)"}

移除資料表參照

作業 方法 URI
移除資料表參照 DELETE [入口網站 URI]/_api/accounts(00000000-0000-0000-0000-000000000002)/opportunity_customer_accounts/$ref?$id=[入口網站 URI]/_api/opportunities(00000000-0000-0000-0000-000000000001)

移除單一值導覽屬性的資料表參照

對於單一值導覽屬性,移除 $id 查詢字串參數。

作業 方法 URI
移除單一值導覽屬性的資料表參照 DELETE [入口網站 URI]/_api/opportunities(00000000-0000-0000-0000-000000000001)/customerid_account/$ref

變更單一值導覽屬性中的參考。

作業 方法 URI JSON 範例
變更單一值導覽屬性中的參考。 PUT [入口網站 URI]/_api/opportunities(00000000-0000-0000-0000-000000000001)/customerid_account/$ref {"@odata.id":"[Portal URI]/_api/accounts(00000000-0000-0000-0000-000000000002)"}

在建立時建立資料表

使用深層插入,可以關聯建立新的資料表。

使用單一值導覽屬性,將表格關聯於更新

您可以使用上述主題中基本更新所描述的相同訊息建立關聯資料表,但您必須使用 @odata.bind 注釋來設定單一值導覽屬性的值。 在以下範例使用 customerid_account 單值瀏覽屬性變更與商機關聯的客戶。

使用單一值導覽屬性,將表格關聯於更新

作業 方法 URI JSON 範例
使用單一值導覽屬性,將表格關聯於更新 PATCH [入口網站 URI]/_api/opportunities(00000000-0000-0000-0000-000000000001) {"customerid_account@odata.bind":"[Portal URI]/_api/accounts(00000000-0000-0000-0000-000000000002)"}

Web API AJAX 範例

此範例示範如何使用非同步 JavaScript 和 XML (AJAX) 來建立、更新及刪除資料表記錄。

包裝函式 AJAX 函數

	(function(webapi, $){
		function safeAjax(ajaxOptions) {
			var deferredAjax = $.Deferred();
	
			shell.getTokenDeferred().done(function (token) {
				// add headers for AJAX
				if (!ajaxOptions.headers) {
					$.extend(ajaxOptions, {
						headers: {
							"__RequestVerificationToken": token
						}
					}); 
				} else {
					ajaxOptions.headers["__RequestVerificationToken"] = token;
				}
				$.ajax(ajaxOptions)
					.done(function(data, textStatus, jqXHR) {
						validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve);
					}).fail(deferredAjax.reject); //AJAX
			}).fail(function () {
				deferredAjax.rejectWith(this, arguments); // on token failure pass the token AJAX and args
			});
	
			return deferredAjax.promise();	
		}
		webapi.safeAjax = safeAjax;
	})(window.webapi = window.webapi || {}, jQuery)

建立​​

	webapi.safeAjax({
		type: "POST",
		url: "/_api/accounts",
		contentType: "application/json",
		data: JSON.stringify({
			"name": "Sample Account"
		}),
		success: function (res, status, xhr) {
      //print id of newly created table record
			console.log("entityID: "+ xhr.getResponseHeader("entityid"))
		}
	});

Update

  webapi.safeAjax({
    type: "PATCH",
    url: "/_api/accounts(00000000-0000-0000-0000-000000000001)",
    contentType: "application/json",
    data: JSON.stringify({
      "name": "Sample Account - Updated"
    }),
    success: function (res) {
      console.log(res);
    }
  });

删除

	webapi.safeAjax({
		type: "DELETE",
		url: "/_api/accounts(00000000-0000-0000-0000-000000000001)",
		contentType: "application/json",
		success: function (res) {
			console.log(res);
		}
  });

關聯

下列範例會將現有的連絡人指派為現有帳戶的主要連絡人。

var record = {};
record[primarycontactid@odata.bind] = "/contacts(00000000-0000-0000-0000-000000000002)"; 

webapi.safeAjax({
  type: "PATCH",
  contentType: "application/json",
  url: "/_api/accounts(00000000-0000-0000-0000-000000000001)",
  data: JSON.stringify(record),
    success: function (data, textStatus, xhr) {
      console.log("Record updated");
    }
});

下一個步驟

作法:使用入口網站 Web API

另請參閱