public MediaItem AddFile(string fileName, string mediaItemName) { string sitecorePath = "/sitecore/media library/Images/Poses/wordpress"; // Create the options Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions(); // Store the file in the database, not as a file options.FileBased = false; // Remove file extension from item name options.IncludeExtensionInItemName = false; // Overwrite any existing file with the same name options.KeepExisting = false; // Do not make a versioned template options.Versioned = false; // set the path options.Destination = sitecorePath + "/" + mediaItemName; // Set the database options.Database = Sitecore.Configuration.Factory.GetDatabase("master"); // Now create the file Sitecore.Resources.Media.MediaCreator creator = new Sitecore.Resources.Media.MediaCreator(); string mediaitempath = sitecorePath + "/" + fileName; Item posesMediaItem = Sitecore.Context.Database.GetItem(mediaitempath); MediaItem mediaItem = null; if (posesMediaItem == null) { mediaItem = creator.CreateFromFile(fileName, options); } return(mediaItem); }
public Item CreateorUpdateMedia(string sitecorePath, string sourcePath, string mediaItemName) { try { Database masterDb = Sitecore.Configuration.Factory.GetDatabase("master"); // ItemUri itemUri = ItemUri.Parse(sitecorePath + "/" + mediaItemName); // Item myItem = Database.GetItem(itemUri); // Create the options Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions(); // Store the file in the database, not as a file options.FileBased = false; // Remove file extension from item name options.IncludeExtensionInItemName = false; // Overwrite any existing file with the same name options.OverwriteExisting = true; // Do not make a versioned template options.Versioned = false; // set the path options.Destination = sitecorePath; options.Destination = options.Destination + "/" + mediaItemName; options.Database = masterDb; // Now create the file Sitecore.Resources.Media.MediaCreator creator = new Sitecore.Resources.Media.MediaCreator(); var mediaItem = masterDb.GetItem(sourcePath); if (mediaItem != null) { FileInfo fi = new System.IO.FileInfo(sourcePath); FileStream fs = fi.OpenRead(); MemoryStream ms = new MemoryStream(); ms.SetLength(fs.Length); fs.Read(ms.GetBuffer(), 0, (int)fs.Length); ms.Flush(); fs.Close(); MediaItem updatedItem = creator.AttachStreamToMediaItem(ms, sitecorePath, mediaItemName, options); if (updatedItem != null) { ms.Dispose(); return(updatedItem); } } else { MediaItem newItem = creator.CreateFromFile(sourcePath, options); if (newItem != null) { return(newItem); } } } catch (Exception ex) { } return(null); }
public MediaItem AddFile(string fileName, string sitecorePath, string mediaItemName) { // Create the options Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions(); // Store the file in the database, not as a file options.FileBased = false; // Remove file extension from item name options.IncludeExtensionInItemName = false; // Overwrite any existing file with the same name // options.KeepExisting = false; // Do not make a versioned template options.Versioned = false; // set the path options.Destination = sitecorePath + "/" + mediaItemName; // Set the database options.Database = Sitecore.Configuration.Factory.GetDatabase("master"); // Now create the file Sitecore.Resources.Media.MediaCreator creator = new Sitecore.Resources.Media.MediaCreator(); MediaItem mediaItem = creator.CreateFromFile(fileName, options); return(mediaItem); }
/// <summary> /// /// </summary> /// <param name="db"></param> /// <param name="importPath"></param> /// <param name="contentPath"></param> /// <returns></returns> public static MediaItem UploadContent(Database db, string importPath, string contentPath) { MediaItem mediaItem = null; Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions(); options.Database = db; options.Language = Sitecore.Globalization.Language.Parse(Sitecore.Configuration.Settings.DefaultLanguage); options.Versioned = Sitecore.Configuration.Settings.Media.UploadAsVersionableByDefault; options.Destination = contentPath; options.KeepExisting = false; // force replacement try { Sitecore.Resources.Media.MediaCreator creator = new Sitecore.Resources.Media.MediaCreator(); mediaItem = creator.CreateFromFile(importPath, options); } catch (Exception ex) { Log.Error(String.Format("Genworth.SitecoreExt.Utilities.ContentEditorHelper:UploadContent, exception creating media item {0}, skipping file. Exception: {1}", importPath.Substring(importPath.LastIndexOf("/")), ex.ToString()), typeof(ContentEditorHelper)); SitecoreExt.Utilities.ContentEditorHelper.MoveFile(importPath, Path.Combine(importPath + ".failed")); } return(mediaItem); }
public MediaItem AddFile(string fileName, string mediaItemName) { string sitecorePath = "/sitecore/media library/Images/Poses/wordpress"; // Create the options Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions(); // Store the file in the database, not as a file options.FileBased = false; // Remove file extension from item name options.IncludeExtensionInItemName = false; // Overwrite any existing file with the same name options.KeepExisting = false; // Do not make a versioned template options.Versioned = false; // set the path options.Destination = sitecorePath + "/" + mediaItemName; // Set the database options.Database = master; // Now create the file Sitecore.Resources.Media.MediaCreator creator = new Sitecore.Resources.Media.MediaCreator(); string mediaitempath = sitecorePath + "/" + fileName; Item posesMediaItem = Sitecore.Context.Database.GetItem(mediaitempath); MediaItem mediaItem = null; if (posesMediaItem == null) { mediaItem = creator.CreateFromFile(fileName, options); } return mediaItem; }