Zerdush
Zerdush

Reputation: 163

Programmatically Creating Image under Media Library

How to create an image under Media Library programmatically?

Upvotes: 4

Views: 3404

Answers (1)

Mark Cassidy
Mark Cassidy

Reputation: 5860

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 :-)

Upvotes: 6

Related Questions