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.

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(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 reference for a complete overview.

Create 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:

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(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.

Update entity definitions

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

RequestResponse

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

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

Delete entity definitions

Entity definitions can be deleted by name or by id:

RequestResponse

await MClient.EntityDefinitions.DeleteAsync(definitionId);

RequestResponse

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

Do you have some feedback for us?

If you have suggestions for improving this article,