I got asked via the other day how to copy images in the Sitecore Media Library using Glass.Mapper.

This is a very simple task to achieve using Glass.Mapper. I have assumed that the media is kept in the database and not the file system. First we need a class to represent our Media Item, the class below shows the minimum number of fields you will need to map:

    //Template - /sitecore/templates/System/Media/Unversioned/Jpeg 
    [SitecoreType(AutoMap = true, TemplateId = "{DAF085E8-602E-43A6-8299-038FF171349F}")]
    public class MediaItem
    {
        [SitecoreField("Mime Type")]
        public virtual string MimeType { get; set; }

        [SitecoreField("__Icon")]
        public virtual string Icon { get; set; }

        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
        public virtual Stream Blob { get; set; }
        public virtual string Extension { get; set; }
        public virtual int Size { get; set; }

        //keep adding other field that you want to copy here
    }

Notice that this class contains a property of type Stream, the Media Library stores the actual file data in a field called Blob and yes Glass.Mapper understands how to extract streams from Sitecore! Next we need a class to represent the folder that will contain all our new images:

    public class Folder
    {
        public virtual Guid Id { get; set; }
    }

Ok the models our done, lets look at the code to copy images:

            var service = new SitecoreService("master");
            
            //this folder will contain all our images
            var folder = service.GetItem<Folder>("/sitecore/media library/Images/ImageCopy");
            
            //get the image we want to copy
            var image = service.GetItem<MediaItem>("/sitecore/media library/Images/ImageCopy/Koala");

            //update the name to the new image name
            image.Name = image.Name + DateTime.Now.ToString("yyyyMMddThhmmss");

            //this creates a new image, note that the ID of the image will be update to point
            //at the new item
            service.Create(folder, image);

            //this code updates the icon for the image
            string iconUrl = "~/media/{0}.ashx?h=16&thn=1&w=16";

            image.Icon = string.Format(iconUrl, image.Id.ToString("N"));

            service.Save(image);

Glass.Mapper makes copying items easy because you can simply grab an item from Sitecore and then call the Create method using the item you just got out of Sitecore and Glass.Mapper will create a new version of it. When you create a new item Glass.Mapper will automatically update the ID property to the ID of the new item.

Notice that this code updates the Name of the item, we do this so that our images have unique names but it isn't required.

Finally the code updates the URL of the icon, we have to do this after the item has been created because we need the ID of the new image for the URL.

Running this code we can my Koala image gets copied with the appropriate Icon:


This demo deals with images that are stored in the database, if you were copying files stored on the hard disk you would need to manually copy the physical files and update the File Path field.

comments powered by Disqus