创建自定义实体。
发布日期: 2017年1月
适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online
本主题说明如何创建称为“银行帐户”自定义的用户所有实体,并为其添加四个不同类型的属性。
也可以创建组织所有的自定义实体。详细信息:实体所有权
备注
除非编辑实体属性来设置“显示此实体的区域”,否则无法在应用程序导航中看到此实体。
本主题内容
创建自定义实体。
将 String 属性添加到自定义实体
将 Money 属性添加到自定义实体
将 DateTime 属性添加到自定义实体
将 Lookup 属性添加到自定义实体
创建自定义实体。
以下示例使用 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.")
将 String 属性添加到自定义实体
以下示例将 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)
将 Money 属性添加到自定义实体
以下示例将 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.")
将 Lookup 属性添加到自定义实体
以下示例使用 CreateOneToManyRequest 创建与 Contact 实体的一对多关系,以便将 LookupAttributeMetadata 属性添加到 Bank Account 实体中。
CreateOneToManyRequest req = new CreateOneToManyRequest()
{
Lookup = new LookupAttributeMetadata()
{
Description = new Label("The referral (lead) from the bank account owner", 1033),
DisplayName = new Label("Referral", 1033),
LogicalName = "new_parent_leadid",
SchemaName = "New_Parent_leadId",
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.Recommended)
},
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 = "lead",
ReferencedAttribute = "leadid",
ReferencingEntity = _customEntityName,
SchemaName = "new_lead_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
使用示例和帮助程序代码
自定义实体元数据
可自定义哪些实体?
检索、更新和删除实体
创建和更新可通过电子邮件发送的实体
创建自定义活动实体
修改实体的图标
修改实体的消息
Microsoft Dynamics 365
© 2017 Microsoft。 保留所有权利。 版权