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();
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?
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.
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.
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.
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
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:
This code works on Umbraco v4 and v6.
Jeroen
Thanks Jeroen,
that looks ace: I already have the DAMP Samples code so I'll dig in.
Rich
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
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
Thanks Jeroen - marked as solved.
Rich Lee
How would you do this in general content not just media? ContentTypeSort's value for Id is a Lazy<int>
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
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.
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?
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.
is working on a reply...