Erstellen einer benutzerdefinierten Tabelle mit Code

Dieses Thema zeigt, wie man programmgesteuert eine benutzerdefinierte benutzereigene Tabelle (Entität) mit dem Namen Bankkonto erstellt und vier verschiedene Spaltentypen (Attribute) hinzufügt.

Sie können auch organisationseigene benutzerdefinierte Tabellen erstellen. Weitere Informationen: Tabellenbesitz

Hinweis

Sie können die benutzerdefinierte Tabelle in der Anwendungsnavigation nicht sehen, es sei denn, die Tabelleneigenschaften werden bearbeitet, um die Bereiche, in denen diese Entität angezeigt wird festzulegen.

Die benutzerdefinierte Tabelle erstellen

Das folgende Codebeispiel verwendet CreateEntityRequest, um die Tabelle (Entität) und das StringAttributeMetadataPrimaryAttribute zu erstellen.

Der _customEntityName-Wert ist “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.");

Hinzufügen einer Zeichenfolgenspalte zur benutzerdefinierten Tabelle

Das folgende Codebeispiel fügt eine Spalte StringAttributeMetadata (Attribut) zur Tabelle Bank Account hinzu.

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);

Hinzufügen einer Geldspalte zur benutzerdefinierten Tabelle

Das folgende Codebeispiel fügt eine Spalte MoneyAttributeMetadata (Attribut) zur Tabelle Bank Account hinzu.

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);

Hinzufügen einer Spalte für Datums- und Zeitangaben zur benutzerdefinierten Tabelle

Das folgende Codebeispiel fügt eine Spalte DateTimeAttributeMetadata (Attribut) zur Tabelle Bank Account hinzu.

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.");

Hinzufügen einer Nachschlagespalte zur benutzerdefinierten Tabelle

Das folgende Codebeispiel verwendet CreateOneToManyRequest, um eine 1:n-Beziehung mit der Tabelle Contact zu erstellen, sodass eine Spalte LookupAttributeMetadata (Attribut) zur Tabelle Bank Account hinzugefügt wird.

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);

Siehe auch

CreateEntityRequest
Anpassen von Tabellendefinitionen
Arbeiten mit Tabellendefinitionen über Code

Hinweis

Können Sie uns Ihre Präferenzen für die Dokumentationssprache mitteilen? Nehmen Sie an einer kurzen Umfrage teil. (Beachten Sie, dass diese Umfrage auf Englisch ist.)

Die Umfrage dauert etwa sieben Minuten. Es werden keine personenbezogenen Daten erhoben. (Datenschutzbestimmungen).