Getting "AllowedTypes" programmatically



How can I get the names of the "AllowedTypes" for a contentarea of a page?

I'm trying to create some documentation for our webeditors, I'd like to create a list of the allowable types for each contentarea of each page type. (Preferably translated using the language files)


Sep 23, 2014 15:40

Hi Pete,

You can use the below code:

var contentTypeRepository = ServiceLocator.Current.GetInstance();

            var stringBuilder = new StringBuilder();
            var contentTypes = contentTypeRepository.List(); // Retrieve all content types
            foreach (var item in contentTypes)
                if (item.ModelType != null)
                    var contentAreaProperties = item.ModelType.GetProperties().Where(p => p.PropertyType == typeof (ContentArea)); // Get all content area property types
                    foreach (var contentArea in contentAreaProperties)
                        var attribute = contentArea.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof (AllowedTypesAttribute)); // Get attributes from property
                        if (attribute != null)
                            foreach (var value in attribute.ConstructorArguments)
                                var allowedTypes = 
                                    (from blockType in (ReadOnlyCollection) value.Value 
                                     select ((Type) blockType.Value).Name).ToList();

                                stringBuilder.AppendFormat("Page type: {0}, ContentArea property: {1}, Allowed types: {2}", item.Name, contentArea.Name, string.Join(", ", allowedTypes));
Sep 25, 2014 11:45

Thanks a million, works perfectly.

May I ask how you figured this out? Is there some documentation on the episerver architecture that I haven't found? Lots of trial and error? Sheer brilliance? Or should it have been obvious?

Sep 26, 2014 10:01

Yes there's plenty of documentation about the architecture: You could also download the Alloy Tech demo site for a lot of best practices. And the rest is just experience;)

Sep 26, 2014 10:14

@Patrick: in an ironic twist, clicking your link leads to a 404 :-)

Here's the correct clickable link:

Sep 26, 2014 12:50
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.