1
votes

So I'm attempting to utilize the Content Type Picker as a parameter for a macro. When adding the macro to a page, I select the content type with alias "myType".

How do I retrieve the alias of the document type in the partial view of the macro?

Currently, I am using:

var type = Model.MacroParameters["myType"];

which gives the ID of the document type. Can I retrieve the alias of the document type using the ID?

When using:

@Umbraco.Content(type)

It returns an empty Content object.

2

2 Answers

2
votes

I have found a solution that retrieves the document type alias using the content type picker data type:

//get current content type service
var myContentTypeService = ApplicationContext.Current.Services.ContentTypeService;

//get ID of selected content type
int typeID = Convert.ToInt32(Model.MacroParameters["myType"].ToString());

//get content type object using ID
IContentType myContentType = myContentTypeService.GetContentType(typeID);

//retrieve alias
String alias = myContentType.Alias;
-1
votes

Your method uses the content service, which hits the database, which you don't want on the front end! In your partial, use the following instead:

//get content
var page = Umbraco.TypedContent(Convert.ToInt32(Model.Macroparameters["myType"]));
//get Doctype Alias
var alias = page.DocumentTypeAlias

As a general rule, you should avoid using the content service for front end display, and use the TypedContent method of the Umbraco helper instead! Hope that helps :)