Add images in Sitecore By C# Programmatically.


Hi All,

This is my first blog so i am little bit scared about that. the main purpose to write here whatever i learned i will be explained here so that any body come and take it. I am trying to put Each&Every exception whatever i faced during development.

Recently i have done migrate some images so let's see how to add images in Sitecore by c#.

say for example you have multiple images and you want upload all images in Sitecore node. so i would suggest for the best option to through the scheduler and create one function in our c# code.

let's start so firstly we have add folder under media library to store image. and pick the folder id/path to add image.

so for that we have to take care some point

a. use correct path to read images from your local drive.
b. use correct path to store in Sitecore.


in below image you can see Blog folder where we are adding images.





create a method to do that..

try
            {
                string[] fileEntries = Directory.GetFiles(@"D:/Portals/");  ///Read all images from local drive mention path

                if (fileEntries.Length > 0)
                {
                    foreach (string fileName in fileEntries)
                    {
                        string filename = Path.GetFileNameWithoutExtension(fileName);
                        string fileExtension = Path.GetExtension(fileName);
                        // Create the options
                        Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions();
                        options.FileBased = false;
                        options.IncludeExtensionInItemName = false;
                        // options.KeepExisting = false;
                        options.Versioned = false;
                        options.AlternateText = fileName;
                        options.Destination = "/sitecore/media library/Project/Blog/Blog" + "/" + filename;
                        options.Database = Sitecore.Configuration.Factory.GetDatabase("master");

                        ///Convert into stream to save.
                        FileInfo fileInfo = new System.IO.FileInfo(fileName);
                        FileStream fileStream = fileInfo.OpenRead();
                        MemoryStream memoryStream = new MemoryStream();
                        memoryStream.SetLength(fileStream.Length);
                        fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
                        memoryStream.Flush();
                        fileStream.Close();

                        // 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(memoryStream, fileName, options);
                        memoryStream.Dispose();

                        ///return image id which is created by sitecore
                        string id = mediaItem.ID.ToString();
                    }
                }




so simple put this code and run. and if you set all things correctly then you see all files under your folder. enjoy&cheers.



Happy Coding😇




Comments

Popular posts from this blog

Difference between shared, Versioned and Unversioned fileds in sitecore

How to find broken links in Sitecore.

Setup First Sitecore Helix Example From Scratch (Blank solution)