因此,我正在创建一个内容导入器,以将内容从外部源提取到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,以便将其分配给我的页面?
答案 0 :(得分:1)
假设有以下型号:
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);
}