Adding a file to the Sitecore Media Library programatically

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();
fs.Read(ms.GetBuffer(), 0, (int)fs.Length);
// 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);

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Sitecore 5, Sitecore 6 and tagged , , , . Bookmark the permalink.

9 Responses to Adding a file to the Sitecore Media Library programatically

  1. Pingback: Replacing mediaitems in Sitecore « Brian Pedersen’s Sitecore and .NET Blog

  2. Pingback: Replacing mediaitems in Sitecore | CMS News Today

  3. Nick V says:


    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 ?



  4. Nick V says:

    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…


  5. Pingback: Quick tip: Adding Media Items from a URL | monoco.code

  6. Ben Sewards says:

    Line 20 on MediaItem AddFile(), the instance mediaFile should have fileName as the first parameter :)

  7. Pingback: Adding a language specific file to the Sitecore Media Library programmatically | Sitecore basics!

  8. Anas says:

    Nice article Thanks :)

  9. Clem says:

    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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s