Content Hub Under The Hood

SITECORE CONTENT HUB – Creation of entities Programmatically

As we are advancing on my blog series “Content about Content Hub”, hereI am illustrating how to create Entities programmatically by utilizing APIs. Earlier, I elaborated upon Getting started with Web Client SDK and Authenticating with APIs.

Content Hub provides the Web Client SDK which in turn provides an Entities client to execute CRUD operations on entities such as read, create, update, and delete. The detailed information about the classes and the objects used by Web Client SDK can be located at https://docs.stylelabs.com/contenthub/3.4.x/content/api-reference/index.html

Read / Get Entities

The Web Client SDK provides multiple alternatives to load an entity either by id OR by identifier OR by definition. The “Get Entity” endpoint returns the requested entity in JSON format. The JSON output can be limited to specific members using the ‘members’ parameters in the query string.

The subsequent code can be utilized to obtain an entity by using id:

NOTE: The method(GetAsync) returns null if either entity does not exist or the user does not have enough permissions to read it.

Create Entities

While working with C# code, the EntityFactory will be used to instantiate an Entity. To create an entity, it should be first created locally and then sent to the server for validation and persistence. The code as depicted below will be used to create an entity locally.

Post execution of the above code, execute the beneath shown code for persisting an entity on the server.

The assetId returned from the SaveAsync method is the id of the newly created entity.

Update Entities

To update any entity in Content Hub, the two actions cited below must be performed:

1. Get Entity – It is the same as described in the above Read/Get Entities section. This method will return the entire entity basis on the id provided.

2. Update properties for the fetched entity. As an illustration, in the beneath code, the description property will be updated to “This is a generic image”

3. Save Entity – Similar to creating entities, utilize the save method on the entities client after updating properties.

Delete Entities

To delete any of the entities a method anointed “DeleteAsync” will be utilized. This method takes the entity id that requires to be deleted as an input parameter. To code snippet depicted below will delete the specified entity.

Using a Browser OR Postman tool

All the CRUD operations illustrated above can also be performed by Using a browser OR by utilizing a Postman tool. This tool helps in the development and testing of APIs before actually consuming them in the code.

The Content Hub provides different endpoints which can be used for Get/Read, Create, Update and delete operations respectively. The below table shows the operations, API endpoint, input parameter, and output parameter for any specific call.

OperationAPI EndpointInput ParameterOutput
GET/READhttps://{{hostname}}/api/entities/{EntityId}entityIdJSON output
CREATEhttps://{{hostname}}/api/entitiesEntity JSON payloadJSON response with new “id” and “identifier”
UPDATEhttps://{{hostname}}/api/entities/{Entity Id}JSON Body with updated dataThe specified entity will be updated
DELETEhttps://{{hostname}}/api/entities/{Entity Id}JSON data with “operations type” set to “DeleteEntity”The specified entity will be deleted

This is how an entity can be created programmatically or by using provided endpoints for respective APIs in Sitecore Content Hub. I hope you found it useful. Please feel free to reach out to me with your feedback.

Did you find this useful? If so, please share with your connections.

Leave a Reply

Your email address will not be published. Required fields are marked *