In Sitecore 7.5, how do I programmatically create media elements with a language version?

I want to be able to create / update multimedia elements in code, as well as use language version control. Here in more detail. I have a product content item. When this item is saved, I want to be able to generate a PDF version of this item and save it in the media library. If a PDF version already exists in the media library, I need to update it. It is also a multilingual site. Therefore, if someone saves the French version of the product content item, I will need to generate the French version of the PDF and save or update the French version of the linked PDF file in the media library - do not touch any version in another PDF language. I can’t figure out how to do this. The code I currently have is as follows: if I save the English version of the product, it creates the English version of the PDF.But if I keep the French version of the product, it will create a French version of PDF and delete the English version of PDF.

Does anyone know how to do this?

    public static Item AddMediaItem(byte[] fileBuffer, string fullMediaPath, string fileNameWithExtension, string title, Language language)
    {
        try
        {
            var db = Sitecore.Configuration.Factory.GetDatabase("master");
            var options = new MediaCreatorOptions();
            options.FileBased = false;
            options.IncludeExtensionInItemName = false;
            options.KeepExisting = false;
            options.Versioned = true;
            options.Destination = fullMediaPath;
            options.Database = db;
            options.Language = language;

            var creator = new MediaCreator();
            var fileStream = new MemoryStream(fileBuffer);

            var pdfItem = db.GetItem(fullMediaPath, language);
            if (pdfItem != null)
            {
                var updatedItem = creator.AttachStreamToMediaItem(fileStream, fullMediaPath, fileNameWithExtension,
                    options);
                updatedItem.Editing.BeginEdit();
                updatedItem.Fields["Title"].Value = title;
                updatedItem.Editing.EndEdit();
                return updatedItem;
            }
            else
            {
                //Create a new item
                var newItem = creator.CreateFromStream(fileStream, fileNameWithExtension, options);
                newItem.Editing.BeginEdit();
                newItem.Fields["Title"].Value = title;
                newItem.Editing.EndEdit();
                return newItem;
            }
        }
        catch (Exception ex)
        {
            return null;
        }
    }
+4
source share
2 answers

Thanks to @JanBluemink for pointing me in the right direction. I found the right approach in the following article: Sitecore.Resources.Media.MediaCreator removes media versions . I just had to change the code to use MediaManager instead of MediaCreator when upgrading.

    public static Item AddMediaItem(byte[] fileBuffer, string fullMediaPath, string fileNameWithExtension, string title, Language language)
    {
        try
        {
            var db = Sitecore.Configuration.Factory.GetDatabase("master");
            var options = new MediaCreatorOptions();
            options.FileBased = false;
            options.IncludeExtensionInItemName = false;
            options.KeepExisting = false;
            options.Versioned = true;
            options.Destination = fullMediaPath;
            options.Database = db;
            options.Language = language;

            var creator = new MediaCreator();
            var fileStream = new MemoryStream(fileBuffer);

            var pdfItem = db.GetItem(fullMediaPath, language);
            if (pdfItem != null)
            {
                var mediaItem = new MediaItem(pdfItem);
                var media = MediaManager.GetMedia(mediaItem);
                media.SetStream(fileStream, "pdf");

                pdfItem.Editing.BeginEdit();
                pdfItem.Fields["Title"].Value = title;
                pdfItem.Editing.EndEdit();
                return pdfItem;
            }
            else
            {
                //Create a new item
                var newItem = creator.CreateFromStream(fileStream, fileNameWithExtension, options);
                newItem.Editing.BeginEdit();
                newItem.Fields["Title"].Value = title;
                newItem.Editing.EndEdit();
                return newItem;
            }
        }
        catch (Exception ex)
        {
            return null;
        }
    }
+2
source

I had to add a couple more lines to update the media element stored in the File system with the version.

if (mediaItem.FileBased)
        {
            string uniqueFilename = FileUtil.GetUniqueFilename(FileUtil.MakePath(Settings.Media.FileFolder, MediaManager.Creator.GetMediaStorageFolder(mediaItem.ID, fileshortname)));
            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                mediaItem.BeginEdit();
                mediaItem.FilePath = uniqueFilename;
                mediaItem.EndEdit();
            }
        }
        Media media = MediaManager.GetMedia(mediaItem);
        using (FileStream stream = new FileStream(fileName, FileMode.Open))
        {
            media.SetStream(stream, FileUtil.GetExtension(fileshortname));
        }`
0
source

All Articles