Programmatically creating an image in a multimedia library

How to programmatically create an image in the library?

+4
source share
1 answer

Sitecore 6? This code assumes that

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 according to what you want to load, and insert the appropriate path. If what you are downloading comes from the site live - it may be worth downloading the database uploaded to the "master" instead of the context database (which will be "web").

Hope this helps :-)

+5
source

All Articles