Content types can be added either at the root level, under another content type or under a content type container (or folders as they're called in the Umbraco backoffice). The approach for getting a single container is similar to getting a single content type, meaning that you can look up a container - either by its GUID:
or its numeric counterpart:
In the same way as you can get the content types of a container, you can get the child containers of another container. This is done by calling the GetContainers
method with an array of numeric IDs:
Also, if the array is empty, all containers will be returned:
The content type service acts as a "gateway" to Umbraco data for operations which are related to both content types and media types.
Browse the API documentation for IContentTypeService.
Namespace: Umbraco.Cms.Core.Services
Assembly: Umbraco.Core.dll
All samples in this document will require references to the following dll:
Umbraco.Core.dll
All samples in this document will require the following using statements:
For Razor views:
If you wish to use the content type service in a class, you need to specify the IContentTypeService
interface in your constructor:
In Razor views, you can access the content type service through the @inject
directive:
Retrieving content types See examples on how to retrieve content types via the service - either individually or as a collection.
Retrieving content type containers See examples on how to retrieve content type containers (folders) via the service - either individually or as a collection.
A given content type has a few different unique identifier that we can use to look it up via the content type service. For instance, if we know the GUID of the content type, we can look it up like this:
Although the use of a GUID is preferable, you can also use it's numeric ID:
Finally, you can also look up a content type by its alias:
As content types are stored in a hierarchical list with folders (containers), there is also multiple ways to can get content types. If you are looking for a flat list of all content types, you can use the GetAll
method:
In the example above, the method was called without any parameters. The method also has two overloads, which lets you look up a collection fo content types by either specifying their GUID or numeric IDs:
To get a list of all content types of another content type, you can instead use the GetChildren
method - either by specifying the numeric ID or the GUID:
In some cases it can be useful to check if a content type has children. The HasChildren
method can be used to check whether a content type has children.
Although the use of a GUID is preferable, you can also use it's numeric ID: