Published Date: 13 June, 2021

In Sitecore, everything is an item and for customization we play around items only. Similarly in Sitecore Content Hub, everything is an Entity and it has Entity definitions. In layman language, you can consider entity definition as an entity attributes/ properties.

In this blog, I would talk about how we can create an asset in the Content Hub programmatically.

If you are new for the Sitecore Content Hub, I would suggest reading my earlier few blogs to have some basic understanding about Sitecore Content Hub.

In my earlier blog, you saw that how we could setup WebClient SDK and the code to establish the connection with Content Hub

Create Asset

User M.Asset entity for performing action on Asset. Use below code to create an asset:

IEntity asset = await CHClient.Client().EntityFactory.CreateAsync("M.Asset", CultureLoadOption.Default).ConfigureAwait(false);
asset.Identifier = "custom_indetifier_tool";
asset.SetPropertyValue("Title", "Custom Asset programmatically.");

var assetId = await CHClient.Client().Entities.SaveAsync(asset).ConfigureAwait(false);
Console.WriteLine($"Asset id: {assetId}");

Connection Successfully established.
Create assets
Asset Id: 31437

It returns the asset id which you have uploaded in DAM. You can see your asset/image using the following URL pattern:

https://< sandbox-link>/en-us/asset/31437

http://www.tekkishare.com

Get Asset

We can get the asset details based on asset Id. Use following code to get the asset details:

var query = Query.CreateQuery(entities => from entity in entities where entity.Id == entityId select entity);
IEntity iEntity = await CHClient.Client().Querying.SingleAsync(query);

Connection Successfully established.
Getting asset based on Id: 30832...
Asset Title: Custom Asset programmatically.
Entity Definition: M.Asset

Delete Asset

Delete asset by using following code:

CHClient.Client().Entities.DeleteAsync(31437).ConfigureAwait(false);

Connection Successfully established.
Asset deleted successfully

After delete if you check the asset URL, you will get the 404 page.

Update Asset

Code for updating asset attributes:

await CHClient.Client().Entities.SaveAsync(asset).ConfigureAwait(false);

You can check the entire code below:



References:

Authentication | Sitecore Content Hub Documentation