Entity definitions client
The SDK provides an Entity definitions client to perform CRUD operations on entity definitions.
Adding, removing, or deleting entity definitions can have major consequences and should be done with extreme caution. If in doubt, do not use this client.
The client variable in the following code examples refers to the ContentHubClient instance. When using the JavaScript SDK, the variable name can be chosen freely, but is also called client at instantiation in the documentation.
Getting entity definitions
Entity definitions can be retrieved by name or by ID:
var assetDefinition: IEntityDefinition = await client.entityDefinitions.getAsync("M.Asset");
The assetDefId argument takes a 64-bit number in input:
var assetDefinition: IEntityDefinition = await client.entityDefinitions.getAsync(assetDefId);
When the entity definition does not exist, the method returns null.
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.
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:
import { EntityDefinition } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/base/entity-definition";
import { MemberGroup, StringPropertyDefinition } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/base";
var definition = new EntityDefinition({
name: "M.Demo.Definition",
displayTemplate : "{M.Demo.Definition.Name}"
});
var defaultGroup = new MemberGroup({
name: "M.Demo.Definition.Name",
memberDefinitions: [
new StringPropertyDefinition(
"M.Demo.Definition.Name",
{ isUnique: true }
)
]
});
definition.memberGroups.push(defaultGroup);
var id: number = await client.entityDefinitions.saveAsync(definition);
The returned id is the same as that of the newly created entity definition.
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.
var id: number = await client.entityDefinitions.saveAsync(definition);
The returned id is the same as that of the newly created entity definition.
Deleting entity definitions
Entity definitions can be deleted by name or by ID:
await client.entityDefinitions.deleteAsync("M.Demo.Definition");
The definitionId argument takes a 64-bit number in input:
await client.entityDefinitions.deleteAsync(definitionId);