get list of all pages a specific block is used on

MTM
MTM
Vote:
 

Hi

Using this code I can get all blocks available.

IEnumerable<ContentType> allBlockTypes = contentTypeRepo.List().Where(x => typeof(BlockData).IsAssignableFrom(x.ModelType));

If I iterate this list, how do I get all pages that each block is used on?

#301121
May 03, 2023 12:14
Vote:
 

Get a reference to each instance the block using the content repository (your code only gets the block defintion, not instances of the block) then use something like this:

ContentRepository.GetReferencesToContent(block.ContentLink, false)

#301124
May 03, 2023 14:16
Vote:
 

Do what David wrote, but remember that you will not only get pages, but also other blocks, so you need to traverse until you find a page if that's what you want. If you use the function on a page you will get the places that have links to the page. Since you will get blocks in blocks you might end up with duplicates, so it's a good idea to also do a distinct on the list you get.

#301353
May 08, 2023 8:35
Vote:
 

Hi MTM , you can use the content search capabilities provided by the CMS. Here's an example :

using EPiServer.Find;
using EPiServer.Find.Cms;

// Specify the block type you want to search for
var blockTypeName = "Your.Block.Namespace.YourBlockType";

// Perform a content search to find pages that reference the block type
var searchClient = SearchClient.Instance;
var searchResults = searchClient.Search<PageData>()
    .Filter(x => x.BlockElements().MatchType(blockTypeName))
    .GetContentResult();

// Iterate over the search results to access the pages
foreach (var hit in searchResults)
{
    var page = hit.Document;
    // Process the page as needed
    // For example, you can access properties like page.Name, page.PageLink, etc.
}

in loop you can put the page in in list.

#301903
Edited, May 16, 2023 9:29
* 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.