1

I have a requirement to get all the Content entities from the content hub of the same type of content type.

For example, I have a Blog, Article, and Program type of content in the content hub, and have several content entities of blog, article, and program types. I want to get a list of all the Article types of contents.

I am using Web Client SDK to perform this activity, but not able to find any method that provides the list of all the entities.

Single entity I can retrieve using the Get Entities method.

enter image description here

3 Answers 3

3

You can leverage LINQ query to fetch entities of same type in Content Hub as below.

var query = Query.CreateQuery(entities =>
            from e in entities
            where e.DefinitionName =="Article"
            select e);

IEntityIterator iterator = MClient.Querying.CreateEntityIterator(query);
while (await iterator.MoveNextAsync())
{
   var entities = iterator.Current.Items;
   // Do something with the entities
}

Above query fetches all the entities with DefinitionName as 'Article'. There are multiple ways to filter data in ContentHub using LINQ query through properties and relations. You can find the examples here:

https://doc.sitecore.com/ch/en/developers/cloud-dev/linq-queries-1286073.html

https://doc.sitecore.com/ch/en/developers/cloud-dev/querying-client-1286051.html

Hope this helps!!!

1
  • Thanks, @ckhanna, your answer only provides a single result which is the parent item/type of content. but your answer helped me to get the idea and find the solution. Commented May 27 at 15:17
3

To fetch a list of all Content entities of a specific content type, we can use the following methods

  1. Get the List using 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);

Where 35045 is the Parent ID.

  1. Get the list of all the entities of Content-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;

Refer to the blog post for more detail about other methods and how to find parent ID, etc: Get a List of all Content Entities of the Same Content Type in Sitecore Content Hub

0

You can utilize the following code:

Note: This code may not be 100% accurate, and you may need to modify it according to your requirements.

List<IEntity> ListOfArticle = new List<IEntity>();
var query = Query.CreateQuery(entities => from e in entities
where e.DefinitionName == "M.Content" &&  (e.Property("ContentType") == "Article") select e);
            int take = 10;
            query.Take = take;    
            int timeSpan = 300;
            var scroller = _mclient.Querying.CreateEntityScroller(query, TimeSpan.FromSeconds(timeSpan), EntityLoadConfiguration.DefaultCultureFull);
            if (scroller.CanMoveNext())
            {
                await scroller.MoveNextAsync().ConfigureAwait(false);
                do
                {
                    foreach (IEntity entity in scroller.Current.Items)
                    {
                        ListOfArticle.Add(entry);
                                            }
                    if (!scroller.CanMoveNext())
                    {
                        break;
                    }
                }
                while (await scroller.MoveNextAsync().ConfigureAwait(false));
            }
            return ListOfArticle;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.