/// <summary> /// Imports an image into the root of the Sitecore media library /// </summary> /// <param name="sampleImage"></param> public static void ImportImage(string sampleImage) { FileInfo imageFile = new FileInfo(sampleImage); Item parentItem = Sitecore.Context.Database.GetItem("/sitecore/media library"); var mediaCreatorOptions = new MediaCreatorOptions(); mediaCreatorOptions.Database = Sitecore.Context.Database; mediaCreatorOptions.Language = Sitecore.Context.Language; mediaCreatorOptions.Versioned = false; mediaCreatorOptions.Destination = string.Format("{0}/{1}", parentItem.Paths.FullPath, ItemUtil.ProposeValidItemName(Path.GetFileNameWithoutExtension(sampleImage))); mediaCreatorOptions.FileBased = Sitecore.Configuration.Settings.Media.UploadAsFiles; var mc = new MediaCreator(); mc.CreateFromFile(sampleImage, mediaCreatorOptions); }
public MediaItem AddFile(string fileName, string sitecorePath, string mediaItemName) { Assert.ArgumentNotNullOrEmpty(fileName, nameof(fileName)); // Create the options var options = new MediaCreatorOptions { FileBased = false, IncludeExtensionInItemName = true, OverwriteExisting = true, Versioned = false, Destination = sitecorePath + "/" + mediaItemName, Database = Sitecore.Context.Database }; // Now create the file var creator = new MediaCreator(); var mediaItem = creator.CreateFromFile(fileName, options); return(mediaItem); }
/// <summary> /// Imports an image into the root of the Sitecore media library /// </summary> /// <param name="sampleImage"></param> public static void ImportImage(string sampleImage) { FileInfo imageFile = new FileInfo(sampleImage); Item parentItem = Sitecore.Context.Database.GetItem("/sitecore/media library"); var mediaCreatorOptions = new MediaCreatorOptions(); mediaCreatorOptions.Database = Sitecore.Context.Database; mediaCreatorOptions.Language = Sitecore.Context.Language; mediaCreatorOptions.Versioned = false; mediaCreatorOptions.Destination = string.Format("{0}/{1}", parentItem.Paths.FullPath, ItemUtil.ProposeValidItemName(Path.GetFileNameWithoutExtension(sampleImage))); mediaCreatorOptions.FileBased = Sitecore.Configuration.Settings.Media.UploadAsFiles; var mc = new MediaCreator(); mc.CreateFromFile(sampleImage, mediaCreatorOptions); }