![](/img/trans.png)
[英]Tridion : How can I find out if a page has been published to a particular publication target using the business connector?
[英]EpiServer - How can I find out if a block is being used on any published page?
我有一個Episerver項目,隨着時間的推移,該項目有很多塊,不再需要其中一些塊,是否有辦法查看是否在Episerver網站的任何頁面上使用了創建的塊?
我很少這樣做,但是上一次我使用此代碼來獲取自定義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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.