Entity definitions client

The Entity definitions client can be used to perform CRUD operations on entity definitions.

Warning

Use extreme caution. Adding, removing, or deleting entity definitions can have serious consequences.

Get entity definitions

Entity definitions can be retrieved by name or id:

RequestResponse
IEntityDefinition assetDefinition = await MClient.EntityDefinitions.GetAsync("M.Asset");
RequestResponse
IEntityDefinition assetDefinition = await MClient.EntityDefinitions.GetAsync(<ASSET_DEFINITION_ID>);

If the entity definition does not exist, the method returns null.

Note

There are many more overloads for retrieving entity definitions. It's also possible to resolve names to IDs and vice versa. See the API docs for a complete overview.

Create entity definitions

The SDK lets you create entity definition objects directly, without a factory. A very simple entity definition can be created as follows:

RequestResponse
IEntityDefinition definition = new EntityDefinition
{
  Name = "M.Demo.Definition",
  DisplayTemplate = "{M.Demo.Definition.Name}"
};

var defaultGroup = new MemberGroup { Name = "Default" };
defaultGroup.MemberDefinitions.Add(new StringPropertyDefinition
{
  Name = "M.Demo.Definition.Name",
  IsUnique = true
});

definition.MemberGroups.Add(<DEFAULT_GROUP>);

long id = await MClient.EntityDefinitions.SaveAsync(<DEFINITION>);

The returned id is the ID of the newly created entity definition.

Note

The EntityDefinition can be imported from Stylelabs.M.Sdk.Models.Base, and the property member definitions from Stylelabs.M.Sdk.Models.Base.PropertyDefinitions.

Update entity definitions

Use the save method on the entity definitions client after making a change.

RequestResponse
long id = await MClient.EntityDefinitions.SaveAsync(<DEFINITION>);

This returns the ID of the updated entity definition.

Delete entity definitions

Entity definitions can be deleted by name or by id:

RequestResponse
await MClient.EntityDefinitions.DeleteAsync(<DEFINITION_ID>);
RequestResponse
await MClient.EntityDefinitions.DeleteAsync("M.Demo.Definition");

Do you have some feedback for us?

If you have suggestions for improving this article,