views:

220

answers:

1

Hi, Does any one know how to create an image under Media Library programatically?

Thanks,

A: 

Sitecore 6? This code assumes so

  TemplateItem templateItem = Sitecore.Context.Database.GetTemplate("system/media/unversioned/flash");
  Item parentItem = Sitecore.Context.Database.GetItem( "/sitecore/media library", Language.Parse("en) );
  var mco = new MediaCreatorOptions();
  mco.Database = Sitecore.Context.Database;
  mco.Language = Sitecore.Context.Language;
  mco.Versioned = false;
  mco.Destination = string.Format( "{0}/{1}", parentItem.Paths.FullPath, "my media item name" );
  mco.FileBased = Settings.Media.UploadAsFiles;

  var mc = new MediaCreator();
  newItem = mc.CreateFromFile( "path to your media file", mco );

This is the essence of it. Obviously (or not?), switch the template name to match what you are wanting to upload, and insert an appropriate path. If what you are uploading is coming from the live site - perhaps consider uploading hardwired to "master" database instead of context database (which would be "web").

Hope this helps :-)

Mark Cassidy