Copied to clipboard

Flag this post as spam?

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


  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Aug 26, 2010 @ 15:04
    Jeroen Breuer
    0

    Get DocumentType parent

    Hello,

    Is is possible to get the parent of a DocumentType?

    I've got the following structure of DocumentTypes:

    -Base

    --BasePage

    ---ContentPage

    If I have a document which has the ContentPage as DocumentType (ContentType in API) how can I fetch the parent DocumentType BasePage?

    document.ContentType.Parent returns a System.ArgumentException.

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Aug 28, 2010 @ 13:55
    Jeroen Breuer
    0

    Anyone knows if this can be done?

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 30, 2010 @ 10:09
    Dirk De Grave
    1

    Don't think you can using the api (as you've experienced...) but a short query on the cmsContentType table (column masterContentType) should get you the id of the  parent document type.

     

    Hope this helps.

    Regards,

    /Dirk 

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Aug 30, 2010 @ 10:32
    Jeroen Breuer
    0

    Thanks Dirk. Querying the database won't be a problem and should solve it.

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Aug 30, 2010 @ 14:48
    Jeroen Breuer
    1

    I created a stored procedure which get's all the parents of a DocumentType recursively (including the selected DocumentType). 

    Here is the code:

    WITH UmbracoResults (nodeId, alias, masterContentType) AS
        (
           -- Base case
           SELECT c.nodeId, c.alias, c.masterContentType FROM cmsContentType c
           WHERE nodeId = @nodeId
    
           UNION ALL
    
           -- Recursive step
           SELECT c.nodeId, c.alias, c.masterContentType
           FROM cmsContentType c
            INNER JOIN UmbracoResults -- Note the reference to CTE table name
              ON c.nodeId = UmbracoResults.masterContentType
        )
    
    SELECT * FROM UmbracoResults

    This returns the id, alias and masterId of a DocumentType.

    Jeroen

     

  • Jamie Howarth 306 posts 773 karma points c-trib
    Aug 30, 2010 @ 16:40
    Jamie Howarth
    1

    Hi Jeroen,

    I've done this very recently - you can use:

    DocumentType d = DocumentType.GetByAlias("ContentPage");
    DocumentType dp = new DocumentType(dp.MasterContentType);

    HTH,

    Benjamin

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Aug 30, 2010 @ 16:53
    Jeroen Breuer
    0

    Hi Benjamin,

    Yes that's what I was looking for! I should mark your post as the answer, but I can't. Well for now I'll use my own stored procedure because it can get all the parents in 1 query which is less database intensive.

    Jeroen

  • Jamie Howarth 306 posts 773 karma points c-trib
    Aug 30, 2010 @ 17:20
    Jamie Howarth
    0

    Hey Jeroen,

    No worries, I'll call it a karma IOU ;-)

    Benjamin

  • Raphael Müller 4 posts 34 karma points
    Mar 05, 2014 @ 19:21
    Raphael Müller
    0

    Hi

    I recently used this in Umbraco 7, where the correct way to do this seems to look something like this:

    var contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
    var parentDocumentType = contentTypeService.GetContentType(contentTypeService.GetContentType("ContentPage").ParentId);

    Or recursively:

    var baseDocumentType = contentTypeService.GetContentType("ContentPage");
    while (baseDocumentType.ParentId > 0)
    {
      baseDocumentType = contentTypeService.GetContentType(baseDocumentType.ParentId);
    }

    Does anyone know about the performance of the ContentTypeService? Are the content types cached, or will this result in several database hits?

    Raphael

Please Sign in or register to post replies

Write your reply to:

Draft