分享方式:


建立自訂實體

 

發佈日期: 2016年11月

適用對象: Dynamics CRM 2015

此主題顯示如何建立稱為銀行客戶的使用者擁有實體,並增加四個不同的屬性類型。

您也可以建立屬於組織的自訂實體。其他資訊:實體擁有權

備註

您無法在應用程式中看到這個實體,除非設定了 [顯示這個實體的區域] 實體屬性。

本主題內容

建立自訂實體

新增字串屬性至自訂實體

新增金額屬性至自訂實體

新增 DateTime 屬性至自訂實體

新增查詢屬性至自訂實體

建立自訂實體

下列範例使用 CreateEntityRequest 建立實體以及 StringAttributeMetadataPrimaryAttribute

_customEntityName 值為「new_bankaccount」。


CreateEntityRequest createrequest = new CreateEntityRequest
{

 //Define the entity
 Entity = new EntityMetadata
 {
  SchemaName = _customEntityName,
  DisplayName = new Label("Bank Account", 1033),
  DisplayCollectionName = new Label("Bank Accounts", 1033),
  Description = new Label("An entity to store information about customer bank accounts", 1033),
  OwnershipType = OwnershipTypes.UserOwned,
  IsActivity = false,

 },

 // Define the primary attribute for the entity
 PrimaryAttribute = new StringAttributeMetadata
 {
  SchemaName = "new_accountname",
  RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  MaxLength = 100,
  FormatName = StringFormatName.Text,
  DisplayName = new Label("Account Name", 1033),
  Description = new Label("The primary attribute for the Bank Account entity.", 1033)
 }

};
_serviceProxy.Execute(createrequest);
Console.WriteLine("The bank account entity has been created.");

Dim createrequest As CreateEntityRequest = New CreateEntityRequest With {
 .Entity = New EntityMetadata With {
  .SchemaName = _customEntityName,
  .DisplayName = New Label("Bank Account", 1033),
  .DisplayCollectionName = New Label("Bank Accounts", 1033),
  .Description = New Label("An entity to store information about customer bank accounts", 1033),
  .OwnershipType = OwnershipTypes.UserOwned,
  .IsActivity = False},
 .PrimaryAttribute = New StringAttributeMetadata With {
  .SchemaName = "new_accountname",
  .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  .MaxLength = 100,
  .Format = StringFormat.Text,
  .DisplayName = New Label("Account Name", 1033),
  .Description = New Label("The primary attribute for the Bank Account entity.", 1033)
 }
}
'Define the entity
' Define the primary attribute for the entity
_serviceProxy.Execute(createrequest)
Console.WriteLine("The bank account entity has been created.")

新增字串屬性至自訂實體

下列範例將新增 StringAttributeMetadata 屬性至 Bank Account 實體。


CreateAttributeRequest createBankNameAttributeRequest = new CreateAttributeRequest
{
 EntityName = _customEntityName,
 Attribute = new StringAttributeMetadata
 {
  SchemaName = "new_bankname",
  RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  MaxLength = 100,
  FormatName = StringFormatName.Text,
  DisplayName = new Label("Bank Name", 1033),
  Description = new Label("The name of the bank.", 1033)
 }
};

_serviceProxy.Execute(createBankNameAttributeRequest);

Dim createBankNameAttributeRequest As CreateAttributeRequest = New CreateAttributeRequest With {
 .EntityName = _customEntityName,
 .Attribute = New StringAttributeMetadata With {
  .SchemaName = "new_bankname",
  .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  .MaxLength = 100,
  .Format = StringFormat.Text,
  .DisplayName = New Label("Bank Name", 1033),
  .Description = New Label("The name of the bank.", 1033)
 }
}

_serviceProxy.Execute(createBankNameAttributeRequest)

新增金額屬性至自訂實體

下列範例將新增 MoneyAttributeMetadata 屬性至 Bank Account 實體。


CreateAttributeRequest createBalanceAttributeRequest = new CreateAttributeRequest
{
 EntityName = _customEntityName,
 Attribute = new MoneyAttributeMetadata
 {
  SchemaName = "new_balance",
  RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  PrecisionSource = 2,
  DisplayName = new Label("Balance", 1033),
  Description = new Label("Account Balance at the last known date", 1033),

 }
};

_serviceProxy.Execute(createBalanceAttributeRequest);

Dim createBalanceAttributeRequest As CreateAttributeRequest = New CreateAttributeRequest With {
 .EntityName = _customEntityName,
 .Attribute = New MoneyAttributeMetadata With {
  .SchemaName = "new_balance",
  .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  .PrecisionSource = 2,
  .DisplayName = New Label("Balance", 1033),
  .Description = New Label("Account Balance at the last known date", 1033)
 }
}

_serviceProxy.Execute(createBalanceAttributeRequest)

新增 DateTime 屬性至自訂實體

下列範例將新增 DateTimeAttributeMetadata 屬性至 Bank Account 實體。


CreateAttributeRequest createCheckedDateRequest = new CreateAttributeRequest
{
 EntityName = _customEntityName,
 Attribute = new DateTimeAttributeMetadata
 {
  SchemaName = "new_checkeddate",
  RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  Format = DateTimeFormat.DateOnly,
  DisplayName = new Label("Date", 1033),
  Description = new Label("The date the account balance was last confirmed", 1033)

 }
};

_serviceProxy.Execute(createCheckedDateRequest);
Console.WriteLine("An date attribute has been added to the bank account entity.");

Dim createCheckedDateRequest As CreateAttributeRequest = New CreateAttributeRequest With {
 .EntityName = _customEntityName,
 .Attribute = New DateTimeAttributeMetadata With {
  .SchemaName = "new_checkeddate",
  .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  .Format = DateTimeFormat.DateOnly,
  .DisplayName = New Label("Date", 1033),
  .Description = New Label("The date the account balance was last confirmed", 1033)
 }
}

_serviceProxy.Execute(createCheckedDateRequest)
Console.WriteLine("An date attribute has been added to the bank account entity.")

新增查詢屬性至自訂實體

下列範例使用 CreateOneToManyRequest 建立一對多關聯搭配 Contact 實體,所以 LookupAttributeMetadata 屬性會加至 Bank Account 實體。


CreateOneToManyRequest req = new CreateOneToManyRequest()
{
 Lookup = new LookupAttributeMetadata()
 {
  Description = new Label("The owner of the bank account", 1033),
  DisplayName = new Label("Account Owner", 1033),
  LogicalName = "new_parent_contactid",
  SchemaName = "New_Parent_ContactId",
  RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired)
 },
 OneToManyRelationship = new OneToManyRelationshipMetadata()
 {
  AssociatedMenuConfiguration = new AssociatedMenuConfiguration()
  {
   Behavior = AssociatedMenuBehavior.UseCollectionName,
   Group = AssociatedMenuGroup.Details,
   Label = new Label("Bank Accounts", 1033),
   Order = 10000
  },
  CascadeConfiguration = new CascadeConfiguration()
  {
   Assign = CascadeType.Cascade,
   Delete = CascadeType.Cascade,
   Merge = CascadeType.Cascade,
   Reparent = CascadeType.Cascade,
   Share = CascadeType.Cascade,
   Unshare = CascadeType.Cascade
  },
  ReferencedEntity = Contact.EntityLogicalName,
  ReferencedAttribute = "contactid",
  ReferencingEntity = _customEntityName,
  SchemaName = "new_contact_new_bankaccount"
 }
};
_serviceProxy.Execute(req);

Dim req As New CreateOneToManyRequest() With {
 .Lookup = New LookupAttributeMetadata() With {
  .Description = New Label("The owner of the bank account", 1033),
  .DisplayName = New Label("Account Owner", 1033),
  .LogicalName = "new_parent_contactid",
  .SchemaName = "New_Parent_ContactId",
  .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired)},
 .OneToManyRelationship = New OneToManyRelationshipMetadata() With {
  .AssociatedMenuConfiguration = New AssociatedMenuConfiguration() With {
   .Behavior = AssociatedMenuBehavior.UseCollectionName,
   .Group = AssociatedMenuGroup.Details,
   .Label = New Label("Bank Accounts", 1033),
   .Order = 10000},
  .CascadeConfiguration = New CascadeConfiguration() With {
   .Assign = CascadeType.Cascade,
   .Delete = CascadeType.Cascade,
   .Merge = CascadeType.Cascade,
   .Reparent = CascadeType.Cascade,
   .Share = CascadeType.Cascade,
   .Unshare = CascadeType.Cascade},
  .ReferencedEntity = Contact.EntityLogicalName,
  .ReferencedAttribute = "contactid",
  .ReferencingEntity = _customEntityName,
  .SchemaName = "new_contact_new_bankaccount"
 }
}
_serviceProxy.Execute(req)

另請參閱

CreateEntityRequest
使用範例和 Helper 程式碼
自訂實體中繼資料
可自訂哪些實體?
擷取、更新和刪除實體
建立和更新可郵寄實體
建立自訂活動實體
修改實體的圖示
修改實體的訊息

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