logo
logo

Entity definitions client

The SDK provides an entity definitions client to perform CRUD operations on entity definitions.

Warning

Adding, removing or deleting entity definitions can have very large consequences and should be done with extreme caution. With great power comes great responsibility. When in doubt, ask before doing anything.

Note

The client variable in the following code examples refers to the IMClient instance. When using the Web SDK, the variable name can be chosen freely, but it is also called client at instantiation in the documentation.

Getting entity definitions

Entity definitions can be retrieved by name or id:

IEntityDefinition assetDefinition = await MClient.EntityDefinitions.GetAsync("M.Asset");
IEntityDefinition assetDefinition = await MClient.EntityDefinitions.GetAsync(assetDefId);

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

Note

There are many more overloads for getting one or more entity definitions. It is also possible to resolve names to ids and vice versa. See the API docs for a complete overview.

Creating entity definitions

Entity definition objects can be directly instantiated and do not need a factory. A very simple entity definition can be created like this:

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

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.

Updating entity definitions

Similar to creating entity definitions, use the save method on the entity definitions client after performing the modification.

long id = await MClient.EntityDefinitions.SaveAsync(definition);

The returned id is the same id of the entity definition.

Deleting entity definitions

Entity definitions can be deleted by name or by id:

await MClient.EntityDefinitions.DeleteAsync(definitionId);
await MClient.EntityDefinitions.DeleteAsync("M.Demo.Definition");

Can we improve this article ? Provide feedback