1

I'm using Sitecore.Resources.Media.MediaCreatorOptions and Sitecore.Resources.Media.MediaCreator to programmatically create media items. I've set the MediaCreatorOptions.FileBased property to false yet the item created has the template /sitecore/templates/System/Media/Unversioned/File.

I assumed the item created would use the /sitecore/templates/System/Media/Unversioned/Image template.

Is there a way I can control the MediaCreator to create items with the Image template instead?

My code is executing from the uiUpload pipeline.

For reference, here is the code I'm using:

var mediaCreatorOptions = new MediaCreatorOptions
{
    Database = Sitecore.Configuration.Factory.GetDatabase("master"),
    Destination = item.Paths.Path + "/" + Path.GetFileNameWithoutExtension(mediaItem.MediaPath),
    IncludeExtensionInItemName = false,
    Language = Sitecore.Context.Language,
    Versioned = false,
    FileBased = false,
    AlternateText = Path.GetFileNameWithoutExtension(mediaItem.MediaPath),
    OverwriteExisting = false
};


var creator = new MediaCreator();
MediaItem newMediaItem;

using (new SecurityDisabler())
{
    newMediaItem = creator.CreateFromStream(stream, "test filename", mediaCreatorOptions);
}
1

The template used is dictated by the extension of the MediaCreatorOptions Destination. Look for the mediaLibrary\mediaTypes config section and you'll see how the template you get per extension is configured

<mediaType name="JPEG image" extensions="jpg, jpeg, jpe, jfif">
    <mimeType>image/jpeg</mimeType>
    <forceDownload>false</forceDownload>
    <sharedTemplate>system/media/unversioned/jpeg</sharedTemplate>
    <versionedTemplate>system/media/versioned/jpeg</versionedTemplate>

As your destination has no extension it is using the "*" setting

<mediaType name="Any" extensions="*">
    <mimeType>application/octet-stream</mimeType>
    <forceDownload>true</forceDownload>
    <sharedTemplate>system/media/unversioned/file</sharedTemplate>
    <versionedTemplate>system/media/versioned/file</versionedTemplate>

You already have IncludeExtensionInItemName set to false so keep the extension in the Destination parameter so it knows what template to use.

| improve this answer | |
  • Ah, yeah, that makes sense. I'm converting images to .webp in the uiUpload pipeline and hadn't added it as a mediaType yet. Thanks! – Iceape Dec 18 '19 at 13:28
1

The creator.CreateFromStream selecting the template based on the fileName parameter. So you need add the extension to the fileName parameter like:

newMediaItem = creator.CreateFromStream(stream, "test filename.jpg", mediaCreatorOptions);
| improve this answer | |
  • Thanks for pointing this out! The second parameter wasn't very clear to me what it did. It says it's "filePath" in one place and "fileName" in another. – Iceape Dec 18 '19 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.