Copied to clipboard

Flag this post as spam?

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


  • Rich Lee 26 posts 116 karma points
    Aug 13, 2013 @ 16:18
    Rich Lee
    0

    Modify MediaType allowed child nodes using ContentTypeService

    I posted this earlier from the markdown editor, but it hasn't yet appeared in the forum. Trying again...

    Umbraco 6.1.3, ASP.NET MVC 4, Windows 7, IIS 7 (experienced in Dev with IISExpress)

    Using C# to set up a new Media Type. Need to set the new type as an allowed child node on the Media Type "Folder" node so that a picker (DAMP) can pick that type. In the back-end I expect to see the new type checked under Media Types / Folder / Allowed child node types, and a row in the [cmsContentTypeAllowedContentType] table. 

    It looks like this should be do-able using the AllowedContentTypes on MediaType via the ContentTypeService, however the 3 approaches I can see all fail. Would like advice on how to do this correctly, or confirmation that there's a bug in the Umbraco implementation.

    The new media type is created with:

      imageNewsType = new MediaType(-1)

          {

              Alias = "Image-News",

              AllowedAsRoot = false,

              Description = "media type for news article images",

              Name = "Image - News",

              Icon = "folder.gif",

              Thumbnail = "mediaPhoto.png"

                          };

    Get the "Folder" Media Type with:

        var mediaFolderType = contentTypeService.GetMediaType("Folder");

    and can see that the other Media Types (Folder,Image,File) are all there.

    I've tried:

    *1. AddContentType method using the new type directly:*

        mediaFolderType.AddContentType(imageNewsType);

        contentTypeService.Save(mediaFolderType);

    This runs, but doesn't work.

    *2. AddContentType method using the new type retrieved from the service:*

        var img = contentTypeService.GetMediaType(imageNewsAlias);

        mediaFolderType.AddContentType(img);

        contentTypeService.Save(mediaFolderType);

    This runs, but doesn't work.

    *3. Assign to the AllowedContentTypes property:*

     

      var mediaFolderType = contentTypeService.GetMediaType("Folder");

        var allowedContentTypes = mediaFolderType.AllowedContentTypes.ToList();

        var imageNewsContentType = new ContentTypeSort()

          {

              Alias = imageNewsAlias,

              SortOrder = allowedContentTypes.Count()

          };

      allowedContentTypes.Add(imageNewsContentType);

      mediaFolderType.AllowedContentTypes = allowedContentTypes;

      contentTypeService.Save(mediaFolderType);

    This fails on the Save call with a null reference deep inside Umbraco. YSOD here.

     

    Thanks in advance

    Rich Lee

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Aug 14, 2013 @ 09:59
    Jeroen Breuer
    101

    Hello,

    I don't know how to do it with the new API, but I've got some code from the DAMP Samples package that does work: https://damp.codeplex.com/SourceControl/changeset/view/c591d81084b1#DAMP.Samples/Installer.ascx.cs

    This is how it's done with the old API:

    //Add new MediaTypes to the folder Allowed child nodetypes.
    MediaType folder = MediaType.GetByAlias(FOLDER_MEDIATYPE_NAME);
    
    int[] folderStructure = folder.AllowedChildContentTypeIDs;
    int newsize = folderStructure.Count() + 2;
    
    Array.Resize(ref folderStructure, newsize);
    folderStructure[newsize - 2] = imageWide.Id;
    folderStructure[newsize - 1] = imageLong.Id;
    
    folder.AllowedChildContentTypeIDs = folderStructure;
    folder.Save();

    This code works on Umbraco v4 and v6.

    Jeroen

  • Rich Lee 26 posts 116 karma points
    Aug 14, 2013 @ 13:29
    Rich Lee
    0

    Thanks Jeroen,

    that looks ace: I already have the DAMP Samples code so I'll dig in.

    Rich

  • Rich Lee 26 posts 116 karma points
    Aug 14, 2013 @ 19:50
    Rich Lee
    0

    Hi Jeroen,

    Yes, that works fine thanks, however the MediaType you're using is umbraco.cms.businesslogic.media.MediaType which is obsolete in 6.1.3 in favour of Umbraco.Core.Models.MediaType.

    So it may work, but for how long? What's the steer on using obsoleted classes/methods in v6?

     

    Thanks

    Rich

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Aug 14, 2013 @ 22:52
    Jeroen Breuer
    0

    Hello,

    The old API won't go away soon. It will still be a part of v7 and maybe even v8, but there isn't even a roadmap for that yet. Only old events will stop working in v7, but that is not related to this: http://issues.umbraco.org/issue/U4-2536

    Please mark a post as the solution if it helped you.

    Jeroen

  • Rich Lee 26 posts 116 karma points
    Aug 15, 2013 @ 09:08
    Rich Lee
    0

    Thanks Jeroen - marked as solved.

    Rich Lee

  • Tom 713 posts 954 karma points
    Jan 21, 2014 @ 03:24
    Tom
    0

    How would you do this in general content not just media? ContentTypeSort's value for Id is a Lazy<int>

  • Rich Lee 26 posts 116 karma points
    Jan 21, 2014 @ 11:52
    Rich Lee
    0

    HI Tom,

    I'm not sure where ContentTypeSort comes in? Can you explain?

    My problem was specific to media types, where you need to add a new media type to the default-installed folder type for it to become available. At the time we were using uSiteBuilder to set up the document types and so never hit the issue in doc types. Because uSiteBuilder didn't support media types we only hit this problem in media.

    However we've since moved to using uSync, so we can set up the allowed nodes etc directly in the back end, then use uSync to export the structure to xml files which we then put under source control. When we build a fresh site we run uSync to generate the back end structure from the xml. This works well for us in dev, and we'll use Courier to sync content in production.

    The code-first approach was always problematic for us, as each time we'd have to investigate v6 services, and fall back through v4 services and umbracoHelper. Sometimes we even wrote SQL direct to the database. The Umbraco HQ guys have (rightly IMO) had their attention on v7 Belle rather than the service APIs, and I suspect that in any case our attempted usage isn't a common case and so wouldn't be high priority anyway. And despite Jeroen's optimism about v4APIs sticking around most are now marked as obsolete and definitely represent a risk going forward.

    HTH

    Rich Lee

  • MuirisOG 382 posts 1284 karma points
    Jul 01, 2015 @ 12:23
    MuirisOG
    0

    Hi, and yet again I find myself resurrecting an old post - apologies to everyone.

    I'm hoping that a solution has been found to the ysod mentioned in this post.

    I've hit the same problem - I'm importing media from our old CMS and would like the media types I create to be allowed as child nodes under the "Folder" media type.

  • Tom 713 posts 954 karma points
    Mar 02, 2017 @ 00:22
    Tom
    0

    I'm the same boat. Any idea how to get the folder media type and allow it to create different children of a custom MediaType?

  • MuirisOG 382 posts 1284 karma points
    Mar 02, 2017 @ 10:20
    MuirisOG
    0

    I think I did it manually in the end.

    After I added the media type programmatically, I then logged into the backoffice, and made the changes to where the media type could appear.

Please Sign in or register to post replies

Write your reply to:

Draft