簡體   English   中英

EpiServer-如何確定某個已發布頁面上是否正在使用塊?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM