Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Yoni Cohen 11 posts 71 karma points
    Jan 08, 2015 @ 17:16
    Yoni Cohen
    0

    Get allowed child node types by code

    Hi, I'm trying to get the allowd child node of a document.

    Let's say I have a document Called "Homepage", and in his structure the user can create 3 doc types under it
    "A", "B", "C" .

    How can i get those documents? (DocumentTypeAlias)

     

    Thanks,

  • Dan Lister 416 posts 1974 karma points c-trib
    Jan 12, 2015 @ 14:26
    Dan Lister
    0

    Hi Yoni,

    You could use the Content Type service in order to find a list allowed content types if you know the document type's alias. I haven't tested the following helper method but it should give you an idea as to how you can do it:

    using System.Collections.Generic;
    using System.Linq;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    
    namespace Application
    {
        public class ContentTypeHelper
        {
            public IEnumerable<IContentType> GetAllowedContentTypes(string alias)
            {
                var retval = new List<IContentType>();
    
                if (string.IsNullOrWhiteSpace(alias)) 
                    return retval;
    
                // Use the content type service to find
                // the content type passed to the method
                var contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
                var contentType = contentTypeService.GetContentType(alias);
    
                if (contentType == null) 
                    return retval;
    
                // If we have found the content type, loop through 
                // each of it's allowed content types and select
                // the content type
                retval.AddRange(
                    contentType.AllowedContentTypes.Select(
                        ct => contentTypeService.GetContentType(ct.Id.Value)));
    
                return retval;
            }
        }
    }
    

    So in your instance, you could use the above method as follows:

    var allowed = ContentTypeHelper.GetAllowedContentTypes("Homepage");
    

    Hope that helps.

    Thanks, Dan.

Please Sign in or register to post replies

Write your reply to:

Draft