In the previous part we looked at how to get external information into the Sitecore media library tree using a DataProvider. Usually we use DataProviders to integrate content into the master database and then publish it to the web database for faster handling of that information.

In this case we don’t want to do that because the external media library contains alot of information and we won’t be using everything on the website. So we had to find another way of storing the information and only the used information and nothing more.

We started looking at how Sitecore stores information in the Image field. Many of Sitecores fields stores its information as xml and our idea was to use this and extend the Image fields to store our extra information about the external media. What we did was create a CustomImage class that inherits the Sitecore.Shell.Applications.ContentEditor.Image and “overrided” the BrowseImage method and added a block of code that checks if the selected image is based on any of our external media library templates. If they are we add our extra attributes.

MediaItem item = new MediaItem(innerItem);
TemplateItem template = item.InnerItem.Template;
if ((template != null) && !(this.IsImageMedia(template) || MediaHelper.IsExternalImageMedia(template.ID)))
{
	SheerResponse.Alert("The selected item does not contain an image.", new string[0]);
}
else
{
	MediaUrlOptions options = new MediaUrlOptions();
	string mediaUrl = MediaManager.GetMediaUrl(item, options);

	this.XmlValue.SetAttribute("mediapath", item.MediaPath);
	this.XmlValue.SetAttribute("src", mediaUrl);
	this.XmlValue.SetAttribute("height", item.InnerItem.Fields["height"].Value);
	this.XmlValue.SetAttribute("width", item.InnerItem.Fields["width"].Value);
	if ((template != null) && MediaHelper.IsExternalImageMedia(template.ID))
	{
		this.XmlValue.SetAttribute("extmediaid", item.InnerItem.Fields["ExtMediaId"].Value);
		this.XmlValue.SetAttribute("mediaid", MediaHelper.EmptyImage.ToString());
		this.XmlValue.SetAttribute("extension", item.InnerItem.Fields["Extension"].Value);
	}
	else
		this.XmlValue.SetAttribute("mediaid", item.ID.ToString());

	this.Value = item.MediaPath;
	this.Update();
	this.SetModified();
}

In the code you can see that when it’s an external media item we set the mediaid to MediaHelper.EmptyImage.ToString() which returns an id. The reason for this is that since we don’t publish the external media items to the web database we don’t have any media item to request. Our idea here was to create an empty media node in the media library that we can refer to and request with our information as querystrings when displaying the image.

After creating this class we reconfigured the Sitecore Image Field to use this class instead of the default Image in the Core database. Now we are able to store information about the selected external image. The next step is to use the information.

We tried to extend the Sitecore.Data.Field.ImageField class but found it easier to create a completely new class since there were so many changes to do. What we mainly did was adding the custom attributes. As we went on we found it necessary to extend the Sitecore.Data.Items.MediaItem to add our attributes and to store the Width and Height. I will link these classes at the end of this post.

Now we are able to use the information stored in the field. Next post I will cover the part where we create a new mediahandler that can deliver the image.

Below is a rar-archive with all the classes created.
classes.rar