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;
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.
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
<pre>@Model.MacroParameters["defaultSelection"];</pre>
@{
IPublishedContent test = Umbraco.TypedContent((int)Model.MacroParameters["defaultSelection"]);
}
<pre>@test.Name</pre>
<pre>@test.Url</pre>
<pre>@test.DocumentTypeAlias</pre>
Notes:
Perhaps it's because Umbraco.Content(int) returns a dynamic object, perhaps not. Use TypedContent to get a strongly typed object back.
I found that when using Umbraco.Content or Umbraco.TypedContent it's useful to add the cast to int just in case, as it can cause an exception otherwise...
This only works in the PartialMacroView (i.e. inherited from Umbraco.Web.Macros.PartialViewMacroPage), if you are using the syntax @Html.Action("Index", "MySurface") syntax inside your PVM view to call a SurfaceController, you'll need to pass the MacroParameter to the Surface controller too as its not available by the time you get to the partial underneath the controller...
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:
@{
if (!Model.MacroParameters["defaultSelection"].ToString().IsNullOrWhiteSpace()
&& Model.MacroParameters["defaultSelection"].ToString() != "null")
{
int ContentId;
Int32.TryParse(Model.MacroParameters["defaultSelection"].ToString(), out ContentId);
IPublishedContent test = Umbraco.TypedContent(ContentId);
<pre>@test.Name</pre>
<pre>@test.Url</pre>
<pre>@test.DocumentTypeAlias</pre>
}
else
{
<pre>Parameter is Null</pre>
}
}
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 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.
Using Content Type Picker type as Macro parameter 7.1.8
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
Hey,
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;
Hi Tim,
Thanks for the reply - using the code you provided, docType is returning an empty object, with ID = 0, name = "" and base being empty.
Comment author was deleted
and is content returning anything, like content.Name ?
Comment author was deleted
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.
Comment author was deleted
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.
Comment author was deleted
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
Notes:
@Html.Action("Index", "MySurface")
syntax inside your PVM view to call a SurfaceController, you'll need to pass the MacroParameter to the Surface controller too as its not available by the time you get to the partial underneath the controller...Any help??
Regards,
Danny "Blatant"
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:
@Danny
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:
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...