Sitecore 在玻璃映射器中将 MediaItem 转换为图像
Sitecore convert MediaItem to Image in glass mapper
所以,我正在创建一个内容导入器,以将内容从外部来源拉入 sitecore。
我创建的图像是这样的:
MediaCreatorOptions options = new MediaCreatorOptions();
options.FileBased = false;
options.IncludeExtensionInItemName = false;
options.KeepExisting = false;
options.Versioned = false;
options.Destination = sitecorePath + mediaItemName;
options.Database = Factory.GetDatabase("master");
using (new SecurityDisabler())
{
MediaCreator creator = new MediaCreator();
global::Sitecore.Data.Items.MediaItem mediaItem = creator.CreateFromFile(fileName, options);
}
这确实很愉快地在 sitecore 中创建了媒体项目,我可以在媒体库中浏览它。
下一步是创建实际内容页面。我做这样的事情:
var sitecoreModel = new NewsArticleForImport();
sitecoreModel.Summary = articleContent.Summary;
sitecoreModel.Headline = articleContent.Headline;
using (new SecurityDisabler()) {
masterService.Create(newsRootItem, sitecoreModel);
}
这很好用。
当我想将我的图像分配到我的页面时,问题就来了。因此问题是,如何将 MediaItem 转换为 Glass.Mapper.Sc.Fields.Image,以便将其分配到我的页面?
假设以下模型:
public class NewsFolder
{
public virtual Guid Id { get; set; }
}
[SitecoreType(TemplateId = "{A874228E-B909-4164-8F4A-7DDA5ABA64AB}", AutoMap = true)]
public class NewsItem
{
public virtual Guid Id { get; set; }
public virtual Image Image { get; set; }
public virtual string Name { get; set; }
}
以下代码会将媒体项分配给图像字段:
var database = Sitecore.Configuration.Factory.GetDatabase("master");
var sitecoreService = new SitecoreService(database);
var myNewImage = new MediaItem(database.GetItem("{831D2FDF-98A9-46AD-AFC8-AF4918213068}"));
var parent = sitecoreService.GetItem<NewsFolder>("/sitecore/content/home");
var newNews = new NewsItem();
newNews.Name = "New News";
newNews.Image = new Image();
newNews.Image.MediaId = myNewImage.ID.Guid;
using (new SecurityDisabler())
{
sitecoreService.Create(parent, newNews);
}
所以,我正在创建一个内容导入器,以将内容从外部来源拉入 sitecore。
我创建的图像是这样的:
MediaCreatorOptions options = new MediaCreatorOptions();
options.FileBased = false;
options.IncludeExtensionInItemName = false;
options.KeepExisting = false;
options.Versioned = false;
options.Destination = sitecorePath + mediaItemName;
options.Database = Factory.GetDatabase("master");
using (new SecurityDisabler())
{
MediaCreator creator = new MediaCreator();
global::Sitecore.Data.Items.MediaItem mediaItem = creator.CreateFromFile(fileName, options);
}
这确实很愉快地在 sitecore 中创建了媒体项目,我可以在媒体库中浏览它。
下一步是创建实际内容页面。我做这样的事情:
var sitecoreModel = new NewsArticleForImport();
sitecoreModel.Summary = articleContent.Summary;
sitecoreModel.Headline = articleContent.Headline;
using (new SecurityDisabler()) {
masterService.Create(newsRootItem, sitecoreModel);
}
这很好用。
当我想将我的图像分配到我的页面时,问题就来了。因此问题是,如何将 MediaItem 转换为 Glass.Mapper.Sc.Fields.Image,以便将其分配到我的页面?
假设以下模型:
public class NewsFolder
{
public virtual Guid Id { get; set; }
}
[SitecoreType(TemplateId = "{A874228E-B909-4164-8F4A-7DDA5ABA64AB}", AutoMap = true)]
public class NewsItem
{
public virtual Guid Id { get; set; }
public virtual Image Image { get; set; }
public virtual string Name { get; set; }
}
以下代码会将媒体项分配给图像字段:
var database = Sitecore.Configuration.Factory.GetDatabase("master");
var sitecoreService = new SitecoreService(database);
var myNewImage = new MediaItem(database.GetItem("{831D2FDF-98A9-46AD-AFC8-AF4918213068}"));
var parent = sitecoreService.GetItem<NewsFolder>("/sitecore/content/home");
var newNews = new NewsItem();
newNews.Name = "New News";
newNews.Image = new Image();
newNews.Image.MediaId = myNewImage.ID.Guid;
using (new SecurityDisabler())
{
sitecoreService.Create(parent, newNews);
}