Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Tahir Naveed
Sep 18, 2015
  3603
(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 Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog