Try our conversational search powered by Generative AI!

How can I filter out content usages to remove trashed blocks?

Vote:
 

I'm using this code to get the block data.

var blockType = contentTypeRepository.Load<RequisitionBlock>();
var contentUsages = contentModelUsage.ListContentOfContentType(blockType);
var contentReferences = contentUsages.Select(x => x.ContentLink.ToReferenceWithoutVersion()).Distinct().ToList();
var instances = contentReferences.Select(contentReference => contentLoader.Get<IContent>(contentReference)).ToList();

The usages bring up 9 items and as it gets to instances, it filters to 5 items. But there's only one active item on the site. I've deleted the rest. How can I filter out the deleted blocks?

#310911
Oct 16, 2023 12:57
Vote:
 

You need to check if the blocks are published, here's is how you can do it:

var blockType = contentTypeRepository.Load<RequisitionBlock>();

var contentUsages = contentModelUsage.ListContentOfContentType(blockType);

var contentReferences = contentUsages.Select(x => x.ContentLink.ToReferenceWithoutVersion()).Distinct().ToList();

var instances = new List<IContent>();

foreach (var contentReference in contentReferences)

{

var content = contentLoader.Get<IContent>(contentReference);

var isPublished = contentRepository.TryGet<IContent>(contentReference, out var publishedContent);

if (isPublished)

{

instances.Add(publishedContent);

}

}

#310915
Oct 16, 2023 14:27
eperezjr - Oct 16, 2023 14:54
Hm... I just tried this, but they're all showing as published. Even though I've confirmed all but one are deleted.
Vote:
 

I didn't realize there was an IsDeleted property and this fixes my problem:

var instances = contentReferences
                .Select(contentReference => contentLoader.Get<IContent>(contentReference))
                .Where(content => content != null && !content.IsDeleted)
                .ToList();
#310919
Oct 16, 2023 15:00
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.