I thought it was tricky to read a file from disk and add it to the Sitecore media library untill I actually look into it. Sitecore has introduced 2 classes to help with the creation: The Sitecore.Resources.Media.MediaCreator and the Sitecore.Resources.Media.MediaCreatorOptions.
The Sitecore.Resources.Media.MediaCreatorOptions class is the real gem here, as it helps you pick the right database to write to, but it also creates the folders where the file must be placed. In other words; don’t worry about creating folder items yourself. Let the MediaCreatorOptions do that for you. Here is an example:
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("c:\\myfile.pdf", options); return mediaItem; }
This exampel inserts the myfile.pdf into the media library and stores it as /pdffolder/uploaded/myfile:
MediaItem myFile = AddFile("c:\\myfile.pdf", "myfile", "/pdffolder/uploaded");
The MediaCreator is also available through the static implementation, Sitecore.Resources.Media.MediaManager.Creator, so if you like statics, you can use that one instead.
The MediaCreator can also create media files from a MemoryStream:
// Remember to insert the appropriate try..catch..finally blocks. // I have refrained from using them to compress the example. Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions(); // Now use the options class as I did in the previous example // ... insert code here ... // ... // Read the file from stream: FileInfo fi = new System.IO.FileInfo(fileName); FileStream fs = fi.OpenRead(); MemoryStream ms = new MemoryStream(); ms.SetLength(fs.Length); fs.Read(ms.GetBuffer(), 0, (int)fs.Length); ms.Flush(); fs.Close(); // Now create the file in Sitecore. This time I'll use the static implementation of the MediaCreator Item mediaItem = Sitecore.Resources.Media.MediaManager.Creator.CreateFromStream(ms, fileName, options); ms.Dispose();
Pingback: Replacing mediaitems in Sitecore « Brian Pedersen’s Sitecore and .NET Blog
Pingback: Replacing mediaitems in Sitecore | CMS News Today
Hi,
First of all, great post. However, I keep bumping up against a problem when I try the CreateFromFile. I keep getting the error “Could not create media folder”… I can’t find any info on this anywhere, so I was wondering if you had any ideas ? Could this be a security issue ?
Thanks,
Nick
LikeLike
OK, nevermind, after some trial and error I found the solution… you should stick “/sitecore/media library” before the path you want the file to arrive in the media library, so please ignore my comment…
Nick
LikeLike
Pingback: Quick tip: Adding Media Items from a URL | monoco.code
Line 20 on MediaItem AddFile(), the instance mediaFile should have fileName as the first parameter :)
LikeLike
Pingback: Adding a language specific file to the Sitecore Media Library programmatically | Sitecore basics!
Nice article Thanks :)
LikeLike
Thank you for the post. Nice! Is there any way to populate properties for the media library items when uploading? We have custom properties that we are trying to populate.
LikeLike
We have used similar code but uploaded the media as a file by setting Media.UploadAsFiles as true in configuration. It takes ~ 20 – 40 seconds for media item to get created which keeps end user waiting. Is this much delay expected? We upload the media as file to a common remote file share server so that all cd servers can find it. The media being uploaded here is xml of 6KB size. I have tried uploading the media to default location /App_Data/MediaFiles on same server where sitecore is hosted instead of remote file share server but it stills takes time. Please suggest
LikeLike
Pingback: Which of my old Sitecore posts are still valid in Sitecore 9? | Brian Pedersen's Sitecore and .NET Blog