Umbraco - Changing media type

1.2k views Asked by At

Is there a way to change a media type after it has been created, ideally through c# code, in Umbraco?

Migration by creating a new media node with the new type and migrating images below that media type is turning out more and more problematic, due to references.

Is there a nice way to change, say a folder, to a custom media type?

I am using Umbraco 7 and am looking to perform this migration on the Saved event on the content or media service in the App_Code folder.

2

There are 2 answers

3
Carl Sargunar On BEST ANSWER

I've had to do something very similar with document templates. You have two options - either you can change the media type, or you could create a new media with the correct type, copy properties over and destroy the old one. There should be plenty of code samples to do that but shout if you get stuck

You should just be able to add an event handler in the app_code folder to fire on save

3
David Radick On

I've had a similar need of changing the mediaType associated with nodes. There is the option in the context menu of the Content tree, but nothing similar in the Media tree GUI. Have you found a good way to make this happen and refresh all of the cache/xml associated with them?

I do have suggestion if you went with the route of creating new media nodes to get the new type in place. You mention in a comment, duplicating images to place them as children in the new folder and this messing up references to the images. There is a "Move node" option in the GUI, which will keep the same nodeID for your child images, and thus any references to them. Programatically, I think this would involve updating attributes for the image node, and making sure trees.config gets refreshed.

<ChildImage id="4099" parentID="4098" level="4" creatorID="2" sortOrder="0" createDate="2014-10-16T10:32:07" updateDate="2015-05-05T14:32:15" nodeName="Cutler - 101" urlName="my-image" path="-1,3285,3287,4098,4099" isDoc="" nodeType="2887" writerName="admin" writerID="2" version="03d9a03d-4cc5-4e27-b9ff-6651362e264b" template="0" nodeTypeAlias="ChildImage">

The attributes I believe that would need changing are parentID and path