+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Custom Upload for Images & Documents

Custom Upload for Images & Documents

5 posts, 0 answered
  1. Phill Hodgkinson
    Phill Hodgkinson avatar
    362 posts
    Registered:
    10 Nov 2004
    22 Nov 2010
    Link to this post
    Hi there,

    I'm wondering if there are templates/code available in order to customize the Library Uploader? In particular I want to customize the Image uploader so that it reads Exif data (date taken) and inserts that data into a custom meta field. Can this be done or is this part of the upload functionality locked in compiled code?

    Any help would be greatly appreciated.

    Cheers,
    Phill
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    22 Nov 2010
    Link to this post
    Hello Phill,

    In global.asax you can subscribe for ContentManager.Executed static event and get the item data from the command arguments. Then you can get the IContent object and set its metadata by using SetMetaData method.

    void ContentManager_Executed(object sender, Telerik.ExecutedEventArgs e)
        {
            if (e.CommandName == "UpdateContent" || e.CommandName == "CreateContent")
            {
                var providerName = (e.Data as Telerik.Cms.Engine.Data.CmsContentBase).ProviderName;
                if (providerName == "Libraries")
                {
                    var itemID = e.ItemID;
                    ////
                }
            }
        }

    adding your logic inside the LibraryItemUpload is not possible - first the item is uploaded and then create, so you cannot set the metakey before you upload the item. UploadFile method of the LibraryManager cannot be overridden, so you cannot inject your code there. If you want to work with the RadUpload control in  LibraryItemUpload you should replace the base view and you should not use the built-in RadUpload. You have to create the content item programmatically.

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Phill Hodgkinson
    Phill Hodgkinson avatar
    362 posts
    Registered:
    10 Nov 2004
    23 Nov 2010
    Link to this post
    Hi Ivan,

    Thanks for you're help, you've got me going in the right direction. However I'm stumped again. I've tried the following but get an error "NolnetException: The object belongs already into a different transaction". Any ideas?

    void ContentManager_Executed(object sender, Telerik.ExecutedEventArgs e)
    {
        if(e.CommandName == "UpdateContent"|| e.CommandName == "CreateContent")
        //if (e.CommandName == "CreateContent")
        {
            var providerName = (e.Data as Telerik.Cms.Engine.Data.CmsContentBase).ProviderName;
            if(providerName == "Libraries")
            {
                Telerik.Cms.Engine.IContent libraryItem = e.Data as Telerik.Cms.Engine.IContent;
                libraryItem.SetMetaData("PublishDate", Convert.ToDateTime("1/1/2555"));
                libraryItem.SetMetaData("Author", "Author Name");
    }}}

    Note, this was probably obvious but was missing from previous sample of code:
    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Telerik.Cms.Engine.ContentManager.Executed += new EventHandler<Telerik.ExecutedEventArgs>(ContentManager_Executed);
    }

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    23 Nov 2010
    Link to this post
    Hi Phill,

    You need to get the content again with GetStagedContent because the object is being maintained in another transaction that should release it.

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Phill Hodgkinson
    Phill Hodgkinson avatar
    362 posts
    Registered:
    10 Nov 2004
    23 Nov 2010
    Link to this post
    Hi Ivan,

    Thanks again for your help, greatly appreciated. For anyone who is interested, here's my working code that reads a jpg's EXIF DateTaken field and sets a custom Meta Field "PublishDate" with that value. My client wanted to be able to display a gallery of images, sorted on the date they were taken and capturing the EXIF data on upload is a huge time save.

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Telerik.Cms.Engine.ContentManager.Executed += new EventHandler<Telerik.ExecutedEventArgs>(ContentManager_Executed);
    }
         
    void ContentManager_Executed(object sender, Telerik.ExecutedEventArgs e)
    {
        if (e.CommandName == "CreateContent")
        {
            var providerName = (e.Data as Telerik.Cms.Engine.Data.CmsContentBase).ProviderName;
            if(providerName == "Libraries")
            {
                var itemID = e.ItemID;
                var manager = new ContentManager(providerName);
                var content = (StagedContent)manager.GetStagedContent(itemID);
     
                object data = content.Content;
                if (data is byte[])
                {
     
                    System.Drawing.Image img =
                                System.Drawing.Image.FromStream(new System.IO.MemoryStream(data as byte[]));
     
                    PropertyItem propItem = img.GetPropertyItem(306);
                    DateTime dtaken;
     
                    //Convert date taken metadata to a DateTime object
                    string sdate = Encoding.UTF8.GetString(propItem.Value).Trim();
                    string secondhalf = sdate.Substring(sdate.IndexOf(" "), (sdate.Length - sdate.IndexOf(" ")));
                    string firsthalf = sdate.Substring(0, 10);
                    firsthalf = firsthalf.Replace(":", "-");
                    sdate = firsthalf + secondhalf;
                    dtaken = DateTime.Parse(sdate);
     
                    content.SetMetaData("PublishDate", dtaken);
     
                    manager.SaveContent(content, ContentStatus.Draft);
                }
            }
        }
    }


    Cheers,
    Phill
Register for webinar
5 posts, 0 answered