Entities client
The SDK provides an Entities client to perform CRUD operations on entities.
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. The entityId
argument uses a 64-bit number as input.
Get entities
The following method gets an entity by its ID:
var entity: IEntity = await client.entities.getAsync(entityId);
When the entity does not exist or the user does not have permission to read it, the method returns null
.
There are many options to load entities by ID, by identifier, or by definition. For more information about the different options, please refer to the IEntitiesClient
in the API reference documentation.
It's possible to pass load configurations when loading entities.
Create entities
An entity can only be instantiated using the EntityFactory
.
In the following snippet, an Asset entity is created locally.
import { CultureLoadOption } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/culture-load-option";
var entity: IEntity = await client.entityFactory.createAsync("M.Asset", new CultureLoadOption(["en-US"]));
In the following snippet, the locally created Asset entity is sent to Content Hub to be validated and persisted.
var id: number = await client.entities.saveAsync(entity);
The returned id
is the ID of the newly created entity. To get the latest version of this entity, use the ID to get it from Content Hub again.
Update entities
Similar to creating entities, use the saveAsync
method on the entities client after performing the modification.
var id: number = await client.entities.saveAsync(entity);
The returned id
is the same ID of the entity. To get the latest version of this entity, use the ID to get it from the server again.
Delete entities
Entity deletion can be achieved by using the deleteAsync
method of the entities client. The entity's ID needs to be specified for deletion.
await client.entities.deleteAsync(entityId);