List available types in contentarea

Vote:
 

Is there a way to list all the allowed types within a content area? I have been using the ContentTypeAvailabilityService for pages (GetSetting() then accessing AllowedContentTypeNames). 

I would also like to view/list the available content types for a contentarea so does anyone know if there is a way to do this for a property (specifically a content area)?  

#277002
Mar 23, 2022 15:02
Vote:
 

Hi Gustav, one thing you can do is get the AllowedTypes attribute for the content area property using reflection.

var allowedTypesAttribute = typeof(MyContentType)
    .GetProperty(nameof(MyContentType.ContentAreaProperty))?
    .GetCustomAttribute(typeof(AllowedTypesAttribute)) as AllowedTypesAttribute;

var allowedTypes = allowedTypesAttribute?.AllowedTypes;

The only problem I can see with this is that internally the allowed types are also filtered by user permissions among other things using the ContentTypeAvailabilityService. Sadly some of the methods used for this are not public. However in order to do this yourself you can first get all the available content types using the ListAvailable method. 

var availableTypes = _contentTypeAvailabilityService.ListAvailable("MyContentType", PrincipalInfo.Current.Principal);

And then compare the available types with the allowed types in order to filter out any types that not available to the user. I will leave this bit up to you if this is what you require. Hope this helps 😀

#277319
Edited, Mar 28, 2022 5:53
Vote:
 

Hi, you can do this by getting all the usage of content data which will give you all contents on your site.

here is example code but you need to modify it according to you I've made it generic,

 private IEnumerable<T> GetDataList<T>()
            where T : ContentData
        {
            var objectType = _contentTypeRepository.Load<T>();
            var usages = _contentModelUsage.ListContentOfContentType(objectType)
                .Select(x => x.ContentLink.ToReferenceWithoutVersion())
                .Distinct()
                .Select(x => _contentLoader.Get<IContentData>(x))
                .OfType<T>()
                .ToList();
            return (IEnumerable<T>)usages;
        }
#278122
Apr 08, 2022 10:02
Chris Sharp - Apr 08, 2022 20:19
This is not what Gustav is asking for.
* 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.