【问题标题】:In Sitecore 7.5 how to programmatically create media items with language versioning?在 Sitecore 7.5 中,如何以编程方式创建带有语言版本的媒体项目?
【发布时间】:2015-06-09 12:46:16
【问题描述】:

我希望能够在代码中创建/更新媒体项目并使用语言版本控制。这里有更多细节。我有一个产品内容项目。保存该项目后,我希望能够生成该项目的 PDF 版本并将其保存到媒体库。如果媒体库中已经存在 PDF 版本,我需要能够对其进行更新。此外,这是一个多语言网站。因此,如果有人保存产品内容项目的法语版本,我需要能够生成 PDF 的法语版本,并且只在媒体库中保存/更新相关 PDF 的法语版本 - 不要触及任何其他语言版本的PDF。我似乎无法弄清楚如何做到这一点。我目前拥有的代码执行以下操作:如果我保存产品的英文版本,那么它会创建 PDF 的英文版本。但是,如果我保存产品的法语版本,它会创建 PDF 的法语版本并删除 PDF 的英语版本。

有人知道怎么做吗?

    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;
        }
    }

【问题讨论】:

  • 有未版本化和版本化媒体模板,请务必使用版本化媒体模板。否则媒体是共享字段。
  • 好吧,在我的代码中我根本没有指定模板。我正在使用 MediaCreator,如果您在选项中看到我指定 options.Versioned = true。所以我认为这是正确的。而且我确实注意到创建的项目确实使用了 PDF 的版本化媒体模板。所以我认为那部分是正确的。
  • 非常感谢@JanBluemink 将我指向该帖子。我能够修改我的代码,现在它正在按照我需要的方式工作!
  • 我一直对 Sitecore 感到沮丧的一个原因是,总是很难找到在代码中做某事的“正确”方式。他们的 API 文档还有很多不足之处。通常你必须通过博客文章或 Stack Overflow 寻找你需要的东西。

标签: sitecore sitecore7 sitecore7.5 sitecore-media-library


【解决方案1】:

感谢@JanBluemink 为我指明了正确的方向。我在以下文章中找到了正确的方法:Sitecore.Resources.Media.MediaCreator deletes versions of media。我只需要修改代码以在更新时使用 MediaManager 而不是 MediaCreator。

    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】:

    我不得不添加几行来更新存储在文件系统中的媒体项目,并使用版本控制。

    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));
            }`
    

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多