使用 Web API 建立和更新實體關聯

發行︰ 2017年1月

適用於: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online

Web API 支援使用關聯中繼資料。自訂實體關聯中繼資料中描述的概念也適用於 Web API。

本主題內容

關聯的適用性

建立一對多關聯

建立多對多關聯

更新關聯

刪除關聯

關聯的適用性

在建立實體關聯之前,您必須確認實體是否有資格接受參與關聯。 您可以使用下表中列出的動作判斷適用性。 這些動作對應至實體關聯適用性中描述的組織服務訊息。

Action

描述

CanBeReferenced Action

檢查特定實體是否可以在一對多關聯中成為主要實體 (一)。

CanBeReferencing Action

檢查特定實體是否可以在一對多關聯中成為參照實體 (多)。

CanManyToMany Action

檢查實體是否能參與多對多關聯。

GetValidManyToMany Function

傳回一組可參與多對多關聯的實體。

GetValidReferencedEntities Function

傳回一組可以在一對多關聯中成為主要實體 (一) 的有效實體。

GetValidReferencingEntities Function

傳回一組可以在一對多關聯中成為相關實體 (多) 的有效實體。

建立一對多關聯

當您建立一對多關聯時,會使用 OneToManyRelationshipMetadata EntityType 來定義。 此定義包括查詢屬性,它是使用 LookupAttributeMetadata EntityType 定義,而且也需要複雜屬性,使用 AssociatedMenuConfiguration ComplexTypeCascadeConfiguration ComplexTypeLabel ComplexTypeLocalizedLabel ComplexType。 查詢屬性會設為 OneToManyRelationshipMetadata 物件的 Lookup 單一值導覽屬性,並且使用深層插入同時建立。其他資訊:在單一作業中建立相關實體一對多關聯

如果您想對一對多關聯套用自訂導覽屬性名稱,可以設定 ReferencingEntityNavigationPropertyNameReferencedEntityNavigationPropertyName 屬性的值。

在您產生必要的 JSON 來定義關聯和查詢屬性之後,對 JSON 執行 POST 至 RelationshipDefinitions 實體集。 您必須包含 Microsoft.Dynamics.CRM.OneToManyRelationshipMetadata 的 @odata.type 屬性值,以釐清您要建立的關聯類型,因為此實體集會用來建立多對多關聯。 結果關聯的 URI 會在回覆中傳回。

  • 要求

    POST cc_WebAPI_ServiceURI/RelationshipDefinitions HTTP/1.1
    Accept: application/json
    Content-Type: application/json; charset=utf-8
    OData-MaxVersion: 4.0
    OData-Version: 4.0
    
    {
     "SchemaName": "new_contact_new_bankaccount",
     "@odata.type": "Microsoft.Dynamics.CRM.OneToManyRelationshipMetadata",
     "AssociatedMenuConfiguration": {
      "Behavior": "UseCollectionName",
      "Group": "Details",
      "Label": {
       "@odata.type": "Microsoft.Dynamics.CRM.Label",
       "LocalizedLabels": [
        {
         "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
         "Label": "Bank Accounts",
         "LanguageCode": 1033
        }
       ],
       "UserLocalizedLabel": {
        "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
        "Label": "Bank Accounts",
        "LanguageCode": 1033
       }
      },
      "Order": 10000
     },
     "CascadeConfiguration": {
      "Assign": "Cascade",
      "Delete": "Cascade",
      "Merge": "Cascade",
      "Reparent": "Cascade",
      "Share": "Cascade",
      "Unshare": "Cascade"
     },
     "ReferencedAttribute": "contactid",
     "ReferencedEntity": "contact",
     "ReferencingEntity": "new_bankaccount",
     "Lookup": {
      "AttributeType": "Lookup",
      "AttributeTypeName": {
       "Value": "LookupType"
      },
      "Description": {
       "@odata.type": "Microsoft.Dynamics.CRM.Label",
       "LocalizedLabels": [
        {
         "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
         "Label": "The owner of the account",
         "LanguageCode": 1033
        }
       ],
       "UserLocalizedLabel": {
        "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
        "Label": "The owner of the account",
        "LanguageCode": 1033
       }
      },
      "DisplayName": {
       "@odata.type": "Microsoft.Dynamics.CRM.Label",
       "LocalizedLabels": [
        {
         "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
         "Label": "Account Owner",
         "LanguageCode": 1033
        }
       ],
       "UserLocalizedLabel": {
        "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
        "Label": "Account Owner",
        "LanguageCode": 1033
       }
      },
      "RequiredLevel": {
       "Value": "ApplicationRequired",
       "CanBeChanged": true,
       "ManagedPropertyLogicalName": "canmodifyrequirementlevelsettings"
      },
      "SchemaName": "new_AccountOwner",
      "@odata.type": "Microsoft.Dynamics.CRM.LookupAttributeMetadata"
     }
    }
    
  • 回覆

    HTTP/1.1 204 No Content
    OData-Version: 4.0
    OData-EntityId: cc_WebAPI_ServiceURI/RelationshipDefinitions(d475020f-5d7c-e511-80d2-00155d2a68d2)
    

建立多對多關聯

當您建立多對多關聯時,必須使用 ManyToManyRelationshipMetadata EntityType 來關聯。 此定義包含要建立的交集實體的名稱,以及關聯在應用程式中顯示的方式,使用 AssociatedMenuConfiguration ComplexTypeLabel ComplexTypeLocalizedLabel ComplexType。其他資訊:多對多關聯

如果您想對多對多關聯套用自訂導覽屬性名稱,可以設定 Entity1NavigationPropertyNameEntity2NavigationPropertyName 屬性的值。

在您產生必要的 JSON 來定義關聯之後,對 JSON 執行 POST 至 RelationshipDefinitions 實體集。 您必須包含 Microsoft.Dynamics.CRM.ManyToManyRelationshipMetadata 的 @odata.type 屬性值,以釐清您要建立的關聯類型,因為此實體集會用來建立一對多關聯。 結果關聯的 URI 會在回覆中傳回。

  • 要求

    POST cc_WebAPI_ServiceURI/RelationshipDefinitions HTTP/1.1
    Accept: application/json
    Content-Type: application/json; charset=utf-8
    OData-MaxVersion: 4.0
    OData-Version: 4.0
    
    {
     "SchemaName": "new_accounts_campaigns",
     "@odata.type": "Microsoft.Dynamics.CRM.ManyToManyRelationshipMetadata",
     "Entity1AssociatedMenuConfiguration": {
      "Behavior": "UseLabel",
      "Group": "Details",
      "Label": {
       "@odata.type": "Microsoft.Dynamics.CRM.Label",
       "LocalizedLabels": [
        {
         "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
         "Label": "Account",
         "LanguageCode": 1033
        }
       ],
       "UserLocalizedLabel": {
        "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
        "Label": "Account",
        "LanguageCode": 1033
       }
      },
      "Order": 10000
     },
     "Entity1LogicalName": "account",
     "Entity2AssociatedMenuConfiguration": {
      "Behavior": "UseLabel",
      "Group": "Details",
      "Label": {
       "@odata.type": "Microsoft.Dynamics.CRM.Label",
       "LocalizedLabels": [
        {
         "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
         "Label": "Campaign",
         "LanguageCode": 1033
        }
       ],
       "UserLocalizedLabel": {
        "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
        "Label": "Campaign",
        "LanguageCode": 1033
       }
      },
      "Order": 10000
     },
     "Entity2LogicalName": "campaign",
     "IntersectEntityName": "new_accounts_campaigns"
    }
    
  • 回覆

    HTTP/1.1 204 No Content
    OData-Version: 4.0
    OData-EntityId: cc_WebAPI_ServiceURI/RelationshipDefinitions(420245fa-c77c-e511-80d2-00155d2a68d2)
    

更新關聯

更新實體中所討論,您會使用 HTTP PUT 方法更新關聯,用您想要套用的變更取代現有的定義。 您無法使用 HTTP PATCH 方法編輯個別屬性,如同對於商務資料實體。 就像實體屬性,您應包含 MSCRM.MergeLabels 標頭,其中的值設定為 true 以免覆寫更新中未包含的翻譯標籤,而且您必須先發行自訂,才能在系統中生效。

刪除關聯

若要使用 Web API 刪除關聯,請使用 HTTP DELETE 方法與關聯的 URI。

另請參閱

自訂實體關聯中繼資料
使用 Web API 搭配 Dynamics 365 中繼資料
使用 Web API 查詢中繼資料
依名稱或 MetadataId 擷取中繼資料
使用 Web API 建立和更新實體定義

Microsoft Dynamics 365

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