Quantcast
Channel: Using Optimizely Platforms
Viewing all articles
Browse latest Browse all 8020

Find what pages custom blocks are used on.

$
0
0

Hi there,

Looking for a little assistance here as I'm trying to learn the ropes with EpiServer.

I'm trying to put together some logic to get a list of all our custom blocks and list out every page these blocks are used on. Fortunately all our custom blocks inherit an abstract class called GlobalBlockData.

I've made a good start thus far & been able to get a list of all the blocks in a List of ContentUsage but at this point im stuck. 

Strangely the usages collection (List<ContentUsage>) seems to have the same entry a few times and I can't work out why - I know that a block only exists twice but I see 11 entries - This is only set up in one language so why am I seeing multiples?

From this point I now need to get the absoloute URLs for the apges these are used on.

Is anyone able to help please?

var _contentTypeRepo = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var _contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();
var _linkRepo = ServiceLocator.Current.GetInstance<IContentSoftLinkRepository>();
var _contentRepo = ServiceLocator.Current.GetInstance<IContentRepository>();
//Get all custom blocks which inherit the abstract class GlobalBlockDataIEnumerable<GlobalBlockData> exporters = typeof(GlobalBlockData)
   .Assembly.GetTypes()
   .Where(t => t.IsSubclassOf(typeof(GlobalBlockData)) && !t.IsAbstract)
   .Select(t => (GlobalBlockData)Activator.CreateInstance(t));
     foreach (GlobalBlockData block in exporters)
     {
          Type type = block.GetType();
          ContentType contentType = _contentTypeRepo.Load(type);
          if (! _contentModelUsage.IsContentTypeUsed(contentType))
          {
               //Block Not currently in use
               table.Rows.Add(contentType.Name, "NOT IN USE", "NOT IN USE");
          } 
          else {
               //Block In use....
               IList<ContentUsage> usages = 
                  _contentModelUsage.ListContentOfContentType(contentType);
               foreach (ContentUsage item in usages)
               {
                    IEnumerable<ReferenceInformation> references = 
                      _contentRepo.GetReferencesToContent(item.ContentLink, false);
               // THIS IS WHERE IM STUCK
               }
         }
}

Viewing all articles
Browse latest Browse all 8020

Trending Articles