我有一个episerver项目,随着时间的推移有很多块,不再需要这些块,有没有办法看看我的episerver网站上的任何页面上是否使用了创建的块?
答案 0 :(得分:2)
我很少这样做,但上次我使用此代码来获取自定义CodeBlock
的所有已发布实例
// initiate the repos (use dependency injection instead of the ServiceLocator)
var contentTypeRepository = ServiceLocator.Current.GetInstance<EPiServer.DataAbstraction.IContentTypeRepository>();
var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();
var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
var linkRepository = ServiceLocator.Current.GetInstance<IContentSoftLinkRepository>();
// loading a block type
var blockType = contentTypeRepository.Load(typeof(CodeBlock));
// get usages, also includes versions
IList<ContentUsage> usages = contentModelUsage.ListContentOfContentType(blockType);
List<IContent> blocks = usages.Select(contentUsage =>
contentUsage.ContentLink.ToReferenceWithoutVersion())
.Distinct()
.Select(contentReference =>
repository.Get<IContent>(contentReference)).ToList();
var unusedBlocks = new List<IContent>();
foreach (IContent block in blocks)
{
var referencingContentLinks = linkRepository.Load(block.ContentLink, true)
.Where(link =>
link.SoftLinkType == ReferenceType.PageLinkReference &&
!ContentReference.IsNullOrEmpty(link.OwnerContentLink))
.Select(link => link.OwnerContentLink);
// if no links
if (!referencingContentLinks.Any())
{
unusedBlocks.Add(block);
}
}
您将在unusedBlocks
现在,像往常一样,除非您想隐藏依赖项,否则不要使用ServiceLocator。