Thursday, October 6, 2016

Uploading images programmatically

As we all know, there are three types of image uploading in sitecore:

1- Upload files: which uploads a single or multiple files from the same location.
2- Upload files (advanced): which allows the user to upload using several options like uploading zip files, or overwrite duplicate files.
3- Drag and Drop: which allows the user to drag and drop several files.

What I am going to explain now, is how to upload images using the upload files (advanced) option using code. This way you can loop through a certain directory on your system, editing options for every single image, then uploading it in a certain folder in the Media Library in Sitecore.

Following is a simple code snippet that loops through a certain directory, edits options and uploads them in a certain folder under Media Library:



You can also get the alternate text for example from the item in sitecore by searching for the item that has this image and get the item name to put in the alternate text. This scenario is useful when you are overwriting an already existing image that is used in only one item in sitecore.

Next time I will post blog of how to download images programmatically from sitecore to a certain location on your system.

Hope this is useful!

3 comments: