Quantcast
Channel: ActiveTopics
Viewing all articles
Browse latest Browse all 72689

Modify MediaType allowed child nodes using ContentTypeService

$
0
0

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


Viewing all articles
Browse latest Browse all 72689