Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Phil 54 posts 139 karma points
    Oct 21, 2014 @ 15:41
    Phil
    0

    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

    Oct 21, 2014 @ 16:19

    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;

  • Phil 54 posts 139 karma points
    Oct 21, 2014 @ 16:23
    Phil
    0

    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

    Oct 21, 2014 @ 16:29

    and is content returning anything, like content.Name ?

  • Comment author was deleted

    Oct 21, 2014 @ 16:30

    You can check the cheatsheet to see which properties are available http://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets

  • Phil 54 posts 139 karma points
    Oct 21, 2014 @ 16:30
    Phil
    0

    Content is also empty.

  • Comment author was deleted

    Oct 21, 2014 @ 16:30

    and is there a value in Model.MacroParameters["myType"] ? 

  • Phil 54 posts 139 karma points
    Oct 21, 2014 @ 16:33
    Phil
    0

    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

    Oct 21, 2014 @ 17:25

    That should be the correct code so not sure what is going wrong, are you getting any errors?

  • Phil 54 posts 139 karma points
    Oct 21, 2014 @ 17:34
    Phil
    0

    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?

  • Danny Blatant 91 posts 358 karma points
    Oct 21, 2014 @ 17:45
    Danny Blatant
    0

    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...

    Any help??

    Regards,

    Danny "Blatant"

  • Danny Blatant 91 posts 358 karma points
    Oct 21, 2014 @ 17:55
    Danny Blatant
    0

    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>
      }
    }
    
  • Phil 54 posts 139 karma points
    Oct 22, 2014 @ 09:17
    Phil
    0

    @Danny

    Unfortunately I still get a null object - could this be a bigger issue than it seems at first glance?

  • Phil 54 posts 139 karma points
    Oct 22, 2014 @ 09:35
    Phil
    101

    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;
    

    Appreciate the help, thanks!

  • Danny Blatant 91 posts 358 karma points
    Oct 22, 2014 @ 10:10
    Danny Blatant
    0

    -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.

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Nov 06, 2015 @ 22:37
    Nicholas Westby
    0

    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.

  • Ali Robertson 13 posts 112 karma points
    May 09, 2017 @ 07:22
    Ali Robertson
    0

    Just went through this entire process myself. Thanks for documenting a solution. I wasn't crazy! This is a bloody awful control!

Please Sign in or register to post replies

Write your reply to:

Draft