使用 C# 重命名 Sitecore 媒体项
Renaming Sitecore Media Item using c#
我想重命名或想给上传的媒体项目命名,但它总是上传为未命名的项目(媒体)名称。以下是我的代码。
public static void UploadImage(int customerID,FileUpload fup)
{
using (new SecurityDisabler())
{
var options = new Sitecore.Resources.Media.MediaCreatorOptions
{
AlternateText =customerID.ToString(),
FileBased = false,
IncludeExtensionInItemName = false,
KeepExisting = false,
Versioned = false,
Destination = "/sitecore/media library/temp",
Database = Sitecore.Configuration.Factory.GetDatabase("master")
};
var filepath = HttpContext.Current.Server.MapPath(fup.FileName);
var creator = new MediaCreator();
var mediaItem = creator.CreateFromStream(fup.PostedFile.InputStream, filepath, options);
MediaItem myFile = mediaItem;
myFile.Name = customerID.ToString(); // unable to give becasue it read only
}
}
尝试在重命名之前调用 .InnerItem.Editing.BeginEdit()
并在重命名之后调用 .InnerItem.Editing.EndEdit()
:
myFile.InnerItem.Editing.BeginEdit();
myFile.InnerItem.Name = customerID.ToString();
myFile.InnerItem.Editing.EndEdit();
有关详细信息,请参阅此处的 3.1.5 How to Place an Item in Editing Mode
章 http://sdn.sitecore.net/upload/sitecore6/content_api_cookbook-a4.pdf
我想重命名或想给上传的媒体项目命名,但它总是上传为未命名的项目(媒体)名称。以下是我的代码。
public static void UploadImage(int customerID,FileUpload fup)
{
using (new SecurityDisabler())
{
var options = new Sitecore.Resources.Media.MediaCreatorOptions
{
AlternateText =customerID.ToString(),
FileBased = false,
IncludeExtensionInItemName = false,
KeepExisting = false,
Versioned = false,
Destination = "/sitecore/media library/temp",
Database = Sitecore.Configuration.Factory.GetDatabase("master")
};
var filepath = HttpContext.Current.Server.MapPath(fup.FileName);
var creator = new MediaCreator();
var mediaItem = creator.CreateFromStream(fup.PostedFile.InputStream, filepath, options);
MediaItem myFile = mediaItem;
myFile.Name = customerID.ToString(); // unable to give becasue it read only
}
}
尝试在重命名之前调用 .InnerItem.Editing.BeginEdit()
并在重命名之后调用 .InnerItem.Editing.EndEdit()
:
myFile.InnerItem.Editing.BeginEdit();
myFile.InnerItem.Name = customerID.ToString();
myFile.InnerItem.Editing.EndEdit();
有关详细信息,请参阅此处的 3.1.5 How to Place an Item in Editing Mode
章 http://sdn.sitecore.net/upload/sitecore6/content_api_cookbook-a4.pdf