A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More.
AI OnAI Off
A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More.
Hope this helps:
var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();
var contentType = contentTypeRepository.Load<MyBlock>();
var contentUsages = contentModelUsage.ListContentOfContentType(contentType);
foreach (var contentUsage in contentUsages)
{
// ...
}
Hi Dejan,
Thank you for the example code. I have tried this, but its look like contentUsages contains lot of duplicate items(versions). How to filter or get actual content without version or published version ?
Hi Venkatraman,
There was a onlyPublished flag in EPiServer CMS 7, but not anymore: http://world.episerver.com/documentation/class-library/?documentId=cms/7/f9fa26cc-ab72-084d-893e-35e300c870e6
Here's the modified code. Hope this helps :)
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();
// get all usages of MyBlock, including versions
var contentType = contentTypeRepository.Load<MyBlock>();
var contentUsages = contentModelUsage.ListContentOfContentType(contentType);
// get distinct content references without version
var contentReferences = contentUsages
.Select(x => x.ContentLink.ToReferenceWithoutVersion())
.Distinct()
.ToList();
// fetch data from DB
var instances = contentReferences
.Select(contentReference => contentLoader.Get<IContent>(contentReference))
.ToList();
// remove unpublished
new FilterPublished().Filter(instances);
Hi All,
Through EPiserver API is it possible to get all blocks by its type ?