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.
I created a stored procedure which get's all the parentsof 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.
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.
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?
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
Anyone knows if this can be done?
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
Thanks Dirk. Querying the database won't be a problem and should solve it.
Jeroen
I created a stored procedure which get's all the parents of a DocumentType recursively (including the selected DocumentType).
Here is the code:
This returns the id, alias and masterId of a DocumentType.
Jeroen
Hi Jeroen,
I've done this very recently - you can use:
DocumentType d = DocumentType.GetByAlias("ContentPage");
DocumentType dp = new DocumentType(dp.MasterContentType);
HTH,
Benjamin
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
Hey Jeroen,
No worries, I'll call it a karma IOU ;-)
Benjamin
Hi
I recently used this in Umbraco 7, where the correct way to do this seems to look something like this:
Or recursively:
Does anyone know about the performance of the ContentTypeService? Are the content types cached, or will this result in several database hits?
Raphael
is working on a reply...