Saturday 5 April 2014

Get Media Item File Size in Sitecore

10 comments
I recently got into a situation where I have to return the size of selected pdf file. In Sitecore using Sitecore.Data.Items.MediaItem class, we can access file size, extension, mime type etc of selected media item.

I have written a function GetSitecoreMediaItemSizeInBytes(string itemId) which takes item id of media item and return the size of media item in bytes.
public long GetSitecoreMediaItemSizeInBytes(string itemId)
        {
            long mediaItemSize = 0;
            if (Sitecore.Data.ID.IsID(itemId))
            {
                Item sitecoreItem = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(itemId));
                if (sitecoreItem != null)
                {
                    MediaItem mediaItem = new MediaItem(sitecoreItem);
                    mediaItemSize = mediaItem.Size;                   
                }
                else
                {
                    Sitecore.Diagnostics.Log.Error("Could not find given sitecore media item with id '" + itemId + "' ", this);
                }
            }
            return mediaItemSize;
        }
Code explanation:
  • Sitecore.Data.ID.IsID() : This method takes in a string guid and returns true if it's a valid guid in Sitecore . The string must be 38 characters long, start with a curly brace "{" and end with a curly brace "}".
    For example :
    string itemId = "{AD1495DB-36FC-4B7A-80B6-D6170AB7F3B1}";
  • Sitecore.Data.ID.Parse() : This method takes in a string guid and returns a Sitecore ID object.
  • Sitecore.Context.Database.GetItem() : This method takes in a Sitecore ID object and return a Sitecore Item object.
  • MediaItem mediaItem = new MediaItem(sitecoreItem) : Using this piece of code, we are getting Sitecore Media Item object.
  • mediaItem.Size : Returns size of media item in bytes.
  • mediaItem.Extension : Returns file extension of media item. In my case; it was .pdf.
  • mediaItem.MimeType : Returns Mime Type of media item. In my case; it was application/pdf.
Comments and suggestions are most welcome. Happy coding!

10 comments :

  1. instance.Response.Write(@"alert('"Message"');document.location.href='" url "'; ");

    ReplyDelete
  2. i got that error

    ReplyDelete
  3. Hello Abhinandan,

    Can you please elaborate the error ?

    ReplyDelete
  4. Adnan Riaz Gondal28 May 2014 at 03:23

    Realy nice article, i was looking for this in many days without finding a good tutorial...!! Yhanks alot...

    ReplyDelete
  5. Thanks Adnan. You can also subscribe for new blogs posts and like our facebook page to get new notifications. :)

    ReplyDelete
  6. Muhammad Maqsoodur Rehman31 May 2014 at 13:15

    Great Can i get the same project code in VB.Net please? Thanks

    ReplyDelete
  7. You can convert c# code into VB.NET by online utilities. Check http://converter.telerik.com/ or http://www.developerfusion.com/tools/convert/csharp-to-vb/

    ReplyDelete
  8. Nice article thanks..

    http://www.aspdotnet-pools.com/2014/06/login-form-with-lightbbox-effect-in.html

    ReplyDelete
  9. Hi, Good article and neat code. I have implemented your code but i have 300 images in one gallery. so all the images are in one page. Can you please advice how to add pagination for the thumbnail images. Thanks

    ReplyDelete
  10. Muhammad Maqsoodur Rehman31 December 2014 at 13:25

    Thanks a lot!

    ReplyDelete