使用c#重命名Sitecore媒体项

时间:2015-04-14 10:00:34

标签: c# asp.net sitecore

我想重命名或想要为上传的媒体项目命名,但它总是以未命名的项目(媒体)名称上传。以下是我的代码。

    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

        }
    }

1 个答案:

答案 0 :(得分:3)

尝试在重命名前调用.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

一章