November Happy Hour will be moved to Thursday December 5th.

Getting "AllowedTypes" programmatically

Vote:
 

Hi,

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)

Thanks.

#90995
Sep 23, 2014 15:40
Vote:
 

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));
                            }
                        }
                    }
                }
            }
#91076
Sep 25, 2014 11:45
Vote:
 

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?

#91104
Sep 26, 2014 10:01
Vote:
 

Yes there's plenty of documentation about the architecture: http://world.episerver.com/Documentation/Items/Developers-Guide/EPiServer-CMS/75/. You could also download the Alloy Tech demo site for a lot of best practices. And the rest is just experience;)

#91106
Sep 26, 2014 10:14
Vote:
 

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

Here's the correct clickable link: http://world.episerver.com/Documentation/Items/Developers-Guide/EPiServer-CMS/75/

#91121
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.