Get a List of all Content Entities of the Same Content Type in Sitecore Content Hub
While working on the Content Hub project I received the requirement to get all the Content Entities of a specific Content Type from the Content Hub.
For instance, if your Content Hub contains various types of content entities such as a Blog, Article, and News type and there are multiple entities of each content type. Here I am taking an example of retrieving all the Content Entities of the Blog type.
To get the list of all the content types, you can use Web Client SDK , there is a method to retrieve the single entity using the Get entities method.
Solution:
1. Create a sample console/web application in Visual Studio and add a reference to the Web Client SDK NuGet package (Stylelabs.M.Sdk.WebClient) to your project.
2. Before beginning to write the logic, you should have the Parent ID of the Content Entities that represent the ID of the Content-Type.
To find the Content Type ID or Parent ID of a content entity from the Content Hub interface, follow these steps:
- Go to any of the content entities page by using the Search option from the Main Menu in the Content Hub navigation bar
- Click on the Content Entity of the desired Content-Type or use the filter from the left side section, this action will open the content entity on a new detail page:
- Copy the URL from the browser and update it to get the entity details in JSON format, as below: https://{your-content-hub-instance-url}/api/entities/1516618 To find the parent ID of the content you need to search the ContentTypeToContent relation in this content entity JSON and copy the ID, where 35045 is the Content-Type Id that is Blog Type.
- To verify this, copy the URL and paste it into the browser, this action will provide you the list of all children in JSON format: https://{your-content-hub-instance-url}/api/entities/35045
3. There are several ways to get the list of entities, I am explaining all here, go to the Class and paste the following snippet of the code as per your requirement:
Option A: if you have the parent ID (Content Type ID) of the content entity:
In the above code, you are searching for content type instead of Asset so the Definition name would be “M.Content”. If you use the same code for Assets then the Definition Name would be “M.Asset” and 35045 is the Blog Type ID.
Option B: If you do not have content Type ID and it can vary on different environments like Staging, UAT, or PROD then:
Option C: Get the list of all the entities of Blog Type using parent-children relation:
Happy Sitecoreing 😊
Comments
Post a Comment