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:
var query = Query.CreateQuery(entities => from e in entities | |
where e.DefinitionName == "M.Content" | |
&& e.Parent("ContentTypeToContent") == 35045 | |
select e); | |
var queryResult = await MClient.Client().Querying.QueryAsync(query); |
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:
//Retrieve the Entity of Blog Type | |
var query = Query.CreateQuery(entities => from e in entities | |
where e.Identifier == "M.ContentType.Blog" | |
select e); | |
// Retrieve the Id of the Blog Type | |
var parentId = Convert.ToInt32(await MClient.Client().Querying.SingleIdAsync(query)); | |
//Use the Blog Type Id to get all the Blog type entities | |
var q = Query.CreateQuery(entities => from e in entities | |
where e.Parent("ContentTypeToContent") == parentId | |
select e); | |
//Execute the query | |
IEntityIterator iterator = MClient.Client().Querying.CreateEntityIterator(q); | |
while (await iterator.MoveNextAsync()) | |
{ | |
var entities = iterator.Current.Items; | |
} |
Option C: Get the list of all the entities of Blog Type using parent-children relation:
//Get the Blog Type entity | |
IEntity blogType = await webMClient.Entities.GetAsync("M.ContentType.Blog", EntityLoadConfiguration.Full); | |
if (blogType == null) | |
{ | |
Console.WriteLine("Blog Type is Null"); | |
return; | |
} | |
//Get the Parent to many children relation | |
IParentToManyChildrenRelation parentToManyChildren = blogType.GetRelation<IParentToManyChildrenRelation>("ContentTypeToContent"); | |
if (parentToManyChildren == null) | |
{ | |
Console.WriteLine("Blog Type Children Relation is Null"); | |
return; | |
} | |
//Get entity IDs of all the blog-type entities | |
var entityIds = parentToManyChildren.Children; |
Happy Sitecoreing 😊
Comments
Post a Comment