Skip to content
April 12, 2011 / kiranpatils

Programatically create a folder in the Sitecore Media Library

Challenge:

If you would like to create media items in Media Library pro-grammatically then this post is for you.

Solution:


public MediaItem AddFile(string fileName, string sitecorePath, string mediaItemName)
 {

 Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions();
 mediaItemName = mediaItemName.Substring(0, mediaItemName.LastIndexOf("."));
 // 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;
 using (new SecurityDisabler()) // Use the SecurityDisabler object to override the security settings
 {
 mediaItem = creator.CreateFromFile(fileName, options);
 }
 return mediaItem;
 }

WeblioGraphy:

http://sdn.sitecore.net/upload/sdn5/articles%202/media/media%20facilities/sitecore%20media%20facilities.pdf

Happy Media Creation! 🙂

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: