Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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?
Comment author was deleted
Yes you'll need to fetch the content doc based on the id, you can do that with var content = Umbraco.Content(Model.MacroParameters["myType"]); var doctype = content.DocumentTypeAlias;
Thanks for the reply - using the code you provided, docType is returning an empty object, with ID = 0, name = "" and base being empty.
and is content returning anything, like content.Name ?
You can check the cheatsheet to see which properties are available http://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets
Content is also empty.
and is there a value in Model.MacroParameters["myType"] ?
There is, it is given as 1089, with the row in the umbracoNode table with ID 1089 having the relevant and correct document type alias in the 'text' column.
That should be the correct code so not sure what is going wrong, are you getting any errors?
No, I'm not receiving any errors, just empty objects.
When I hard code the ID of a content item of type alias 'documentType1', then the docType is returned as the correct alias. Could there be a reason why this doesn't work for the type itself?
That's strange, I am using a similar set up on one of my Partial View Macro's. Here's my code with some test values
IPublishedContent test = Umbraco.TypedContent((int)Model.MacroParameters["defaultSelection"]);
A little further, it seems the cast does not work as expected either (not sure why but the parameter is not of type string but of type object (which is a string???). I was getting exceptions when casting so here's the code I changed to:
&& Model.MacroParameters["defaultSelection"].ToString() != "null")
Int32.TryParse(Model.MacroParameters["defaultSelection"].ToString(), out ContentId);
IPublishedContent test = Umbraco.TypedContent(ContentId);
<pre>Parameter is Null</pre>
Unfortunately I still get a null object - could this be a bigger issue than it seems at first glance?
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);
String alias = myContentType.Alias;
Appreciate the help, thanks!
-1 point to me for not reading the question properly.
I gave you a solution for the Content Picker Data Type not the Content Type Picker Data Type. Where I loaded an IPublishedContent you needed to load an IContentType, My Bad.
Good solution though.
Where are you seeing the Content Type Picker data type? I don't see one in my installation of Umbraco 7, and could actually use one at the moment.
Just went through this entire process myself. Thanks for documenting a solution. I wasn't crazy! This is a bloody awful control!
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted