Checking the Size of a Media Item in Database

Recently, I worked on a task wherein I had to return the size of the Media Item of a website in Sitecore.

This is so very easy. It can be found with the help of the following class : Sitecore.Data.Items.MediaItem.

Also, the size is available in Bytes.

A simple code finding out the solution was as follows:

using Sitecore;
using Sitecore.Data.Items;

public string CheckMediaSizeInDB(string mediaItemID)
{
Item myItem = Context.Database.GetItem(mediaItemID);
Diagnostics.Assert.IsNotNull(myItem, "Item with ID '" + mediaItemID + "' does not Exist!");
MediaItem mymediaItem = new MediaItem(myItem);
// The current value is in Bytes!
string MediaSize = mymediaItem.Size.ToString();
  return MediaSize;
}

Happy Sitecoring! 🙂

Advertisements

One thought on “Checking the Size of a Media Item in Database

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