Try our conversational search powered by Generative AI!

Tahir Naveed
Sep 18, 2015
  3613
(4 votes)

Code snippet to get Available Page Types for a specific page type

Couple of days back, I have to write some code to produce an excel sheet listing all the page types and the corresponding page types that are allowed to be created under the page type. 

for each page type in the solution,

    produce a list of page types that are allowed to be created under this page type.

This excel sheet will then be used by QA  guys, as a starting point, to write some test scripts for an EPiServer 6 R2 and composer based solution to EPiServer 8 migration/upgrade project. 

To start with, you need to find all the page types in the solution.

Below is simple function to list all the page types

 private IEnumerable<PageType> GetAllPageTypes()
        {
            var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
            return contentTypeRepository.List().OfType<PageType>();
        }

The following snippet (self explanatory I think) will help you identify all the avaialable page types for a particular page type

 void GetAvailableContentTypes(ContentType contentType)
        {
            var contentTypeAvailablilityService = ServiceLocator.Current.GetInstance<ContentTypeAvailabilityService>();

            // Get Available settings
            AvailableSetting setting = contentTypeAvailablilityService.GetSetting(contentType.Name);
            bool allAvailable = setting.Availability == Availability.All ||
                               setting.Availability == Availability.Undefined &&
                               !setting.AllowedContentTypeNames.Any();


            var availableList = new List<PageType>();
            if (!allAvailable)
            {
                foreach (PageType pageType in this.GetAllPageTypes())
                {
                    foreach (string str in setting.AllowedContentTypeNames)
                    {
                        if (str.Equals(pageType.Name))
                        {
                            availableList.Add(pageType);
                        }
                    }
                }
            }
            if (allAvailable)
            {
                // All the page types are allowed
            }
            else
            {
                // Allowed page types are in the availableList
            }
        }

Hope this will be useful :)

Sep 18, 2015

Comments

Please login to comment.
Latest blogs
Optimizely Forms: Safeguarding Your Data

With the rise of cyber threats and privacy concerns, safeguarding sensitive information has become a top priority for businesses across all...

K Khan | May 16, 2024

The Experimentation Process

This blog is part of the series -   Unlocking the Power of Experimentation: A Marketer's Insight. Welcome back, to another insightful journey into...

Holly Quilter | May 16, 2024

Azure AI Language – Sentiment Analysis in Optimizely CMS

In the following article, I showcase how sentiment analysis, which is part of the Azure AI Language service, can be used to detect the sentiment of...

Anil Patel | May 15, 2024 | Syndicated blog

Optimizely Data Platform Visitor Groups now supports multiple instances

The module V2.0 now supports multiple Optimizely Data Platform instances, allowing personalized content based on real-time segments and profile dat...

Andrew Markham | May 15, 2024 | Syndicated blog

IP block for edit and admin in DXP

Why IP-blocking edit/admin? A hacker can try to guess user names and passwords to gain access to your site. This risk can be minimized in a couple ...

Daniel Ovaska | May 15, 2024

Do not upgrade to EPiServer.CMS.Core 12.21.4 without reading this!

Todays update of EPiServer.CMS.Core 12.21.4 alters default sort order in an unexpected way, when you are working with muligple languages and have...

Tomas Hensrud Gulla | May 14, 2024 | Syndicated blog