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
{
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;
}
}
source
share