More in this section

Forums / Bugs & Issues / Module Builder Image Field

Module Builder Image Field

7 posts, 0 answered
  1. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    03 Aug 2012
    Link to this post
    I put an image field on my type, and edited the backend list view to show the images in a column.

    I then used the code sample to import some data (and images).

    ...but the backend images in the column are always this
    <img src="" />

    And if I look at the front-end auto-generated control, in both details and list view the image is showing up perfectly fine.

    ???
  2. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    05 Aug 2012
    Link to this post
    See attached...
    It clearly has something to do with my import as if I use the UI to upload it all works fine :/

    What am I doing wrong?

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Images.aspx.cs" Inherits="SitefinityWebApp.Sitefinity.Migration.Images" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <h1>Images</h1>
            <asp:TextBox ID="sourceFolder" runat="server" Width="300px" Text="C:\Dropbox\Projects\GourmetSleuth\Gourmet Sleuth\images" />
            <asp:TextBox ID="migrateFromLetter" runat="server" Width="50px" Text="a" />
            <asp:Button ID="migrateButton" runat="server" Text="Migrate Images Items" OnClick="OnMigrateImages_Click" />
            <asp:Button ID="Button1" runat="server" Text="Delete all Images Items" OnClick="OnDeleteImages_Click" />
            <hr />
            <asp:Literal ID="errorsLiteral" runat="server" />
            <asp:Literal ID="resultLiteral" runat="server" />
             
        </div>
        </form>
    </body>
    </html>


    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Sitefinity;
    using Telerik.Sitefinity.Modules.Libraries;
    using Telerik.Web.UI;
     
    namespace SitefinityWebApp.Sitefinity.Migration
    {
        public partial class Images : System.Web.UI.Page {
            int _count = 0;
            int _duplicates = 0;
     
     
            public void OnDeleteImages_Click(object sender, EventArgs e) {
                foreach (var a in this.LibraryManager.GetAlbums())
                {
                    foreach (var image in a.Images())
                    {
                        LibraryManager.DeleteImage(image);
     
                    }
                }
                LibraryManager.SaveChanges();
            }
     
            public void OnMigrateImages_Click(object sender, EventArgs e) {
                if (Directory.Exists(sourceFolder.Text)) {
                    ProcessDir(sourceFolder.Text, 0, "Content");
                }
     
                resultLiteral.Text = "Processed " + _count + " images starting with " + migrateFromLetter.Text + "<br />" + _duplicates + " skipped";
     
                int result = -1;
                if(Int32.TryParse(migrateFromLetter.Text, out result)){
                    if (result < 9)
                        migrateFromLetter.Text = (result + 1).ToString();
                    else
                        migrateFromLetter.Text = "a";
     
                }else if( migrateFromLetter.Text == "a")
                    migrateFromLetter.Text = "b";
                else if (migrateFromLetter.Text == "b")
                    migrateFromLetter.Text = "c";
                else if (migrateFromLetter.Text == "c")
                    migrateFromLetter.Text = "d";
                else if (migrateFromLetter.Text == "d")
                    migrateFromLetter.Text = "e";
                else if (migrateFromLetter.Text == "e")
                    migrateFromLetter.Text = "f";
                else if (migrateFromLetter.Text == "f")
                    migrateFromLetter.Text = "g";
                else if (migrateFromLetter.Text == "g")
                    migrateFromLetter.Text = "h";
                else if (migrateFromLetter.Text == "h")
                    migrateFromLetter.Text = "i";
                else if (migrateFromLetter.Text == "i")
                    migrateFromLetter.Text = "j";
                else if (migrateFromLetter.Text == "j")
                    migrateFromLetter.Text = "k";
                else if (migrateFromLetter.Text == "k")
                    migrateFromLetter.Text = "l";
                else if (migrateFromLetter.Text == "l")
                    migrateFromLetter.Text = "m";
                else if (migrateFromLetter.Text == "m")
                    migrateFromLetter.Text = "n";
                else if (migrateFromLetter.Text == "n")
                    migrateFromLetter.Text = "o";
                else if (migrateFromLetter.Text == "o")
                    migrateFromLetter.Text = "p";
                else if (migrateFromLetter.Text == "p")
                    migrateFromLetter.Text = "q";
                else if (migrateFromLetter.Text == "q")
                    migrateFromLetter.Text = "r";
                else if (migrateFromLetter.Text == "r")
                    migrateFromLetter.Text = "s";
                else if (migrateFromLetter.Text == "s")
                    migrateFromLetter.Text = "t";
                else if (migrateFromLetter.Text == "t")
                    migrateFromLetter.Text = "u";
                else if (migrateFromLetter.Text == "u")
                    migrateFromLetter.Text = "v";
                else if (migrateFromLetter.Text == "v")
                    migrateFromLetter.Text = "w";
                else if (migrateFromLetter.Text == "w")
                    migrateFromLetter.Text = "x";
                else if (migrateFromLetter.Text == "x")
                    migrateFromLetter.Text = "y";
                else if (migrateFromLetter.Text == "y")
                    migrateFromLetter.Text = "z";
     
                migrateButton.Text = "Migrate Next";
            }
     
            private void ProcessDir(string sourceDir, int recursionLvl, string libraryName)
            {
                string[] fileEntries = Directory.GetFiles(sourceDir);
     
                foreach (string fileName in fileEntries) {
                    FileInfo file = new FileInfo(fileName);
                    if (file.Name.StartsWith(migrateFromLetter.Text) || migrateFromLetter.Text == "")
                    {
                        using (FileStream fileStream = File.OpenRead(file.FullName))
                        {
                            MemoryStream memStream = new MemoryStream();
                            memStream.SetLength(fileStream.Length);
                            fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
     
                            this.UploadImage(fileStream, file.Name, file.Extension, file.FullName, libraryName);
                        }
                    }
                }
     
                // Recurse into subdirectories of this directory.
                string[] subdirEntries = Directory.GetDirectories(sourceDir);
                foreach (string subdir in subdirEntries)
                {
                    DirectoryInfo dir = new DirectoryInfo(subdir);
     
                    if (dir.GetFiles().Count() > 0)
                    {
                        var album = LibraryManager.GetAlbums().ToList().Where(a => a.Title == dir.Name).FirstOrDefault();
                        if (album == null)
                        {
                            //Create a new image library
                            App.WorkWith().Album().CreateNew().Do(
                                 a =>
                                 {
                                     a.Title = dir.Name;
                                     a.Description = "";
                                     a.UrlName = Util.GetUrl(dir.Name);
                                     a.BlobStorageProvider = "FileSystem";
                                     album = a;
                                 }
                             ).SaveChanges();
                        }
     
                        // Do not iterate through reparse points
                        if ((File.GetAttributes(subdir) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
                            ProcessDir(subdir, recursionLvl + 1, album.Title);
                    }
                }
            }
     
            public void UploadImage(System.IO.Stream stream, string name, string extension, string fullname, string libraryName)
            {
                try
                {
                    this.LibraryManager.Provider.SuppressSecurityChecks = true;
                    Telerik.Sitefinity.Libraries.Model.Image newImage = LibraryManager.CreateImage();
     
                    //Set its album
                    var album = this.LibraryManager.GetAlbums().FirstOrDefault(x => x.Title == libraryName);
                    if (album.Images().Count(x => x.Title == name) == 0)
                    {
                        newImage.Parent = album;
     
                        newImage.Title = name;
                        newImage.UrlName = Util.GetUrl(name);
     
                        LibraryManager.Upload(newImage, stream, extension);
                        LibraryManager.RecompileItemUrls<Telerik.Sitefinity.Libraries.Model.Image>(newImage);
                        LibraryManager.Lifecycle.Publish(newImage);
                        LibraryManager.SaveChanges();
     
                        _count++;
                    }
                }catch(Exception ex){
                    errorsLiteral.Text = fullname + "<br />";
                }
            }
     
            private LibrariesManager _manager = null;
            public LibrariesManager LibraryManager {
                get {
                    if(_manager == null)
                        _manager = LibrariesManager.GetManager();
                    return _manager;
                }
     
            }
        }
    }


    Notes:
    I know I'm selecting the right image b/c when I edit the item in the UI it shows up right in the edit single item selector screen.

    I edit an item and RE-SELECT an image from the already uploaded, again, it shows up fine in the grid
    Video: http://screencast.com/t/0g99sBkpF8V

    **EDIT** the a,b,c,d thing is just in there b/c there's thousands of images so I need to do it one letter at a tie or I get timeout errors
  3. Georgi Dimitrov
    Georgi Dimitrov avatar
    9 posts
    Registered:
    02 Jun 2017
    08 Aug 2012
    Link to this post
    Hi Steve,

    Thank you for sharing with us your experience.

    I have tested the provided code and it looks fine, there is only a small inaccuracy with the image publishing. It should look like:

    LibraryManager.Upload(newImage, stream, extension);
    LibraryManager.RecompileItemUrls<Telerik.Sitefinity.Libraries.Model.Image>(newImage);
    LibraryManager.SaveChanges();
     
    var bag = new Dictionary<string, string>();
    bag.Add("ContentType", typeof(Telerik.Sitefinity.Libraries.Model.Image).FullName);
    WorkflowManager.MessageWorkflow(newImage.Id, typeof(Telerik.Sitefinity.Libraries.Model.Image), null, "Publish", false, bag);

    About the missing images in the backend grid could you please try to associate an image with dynamic item using the following code snippet:

    var contentLinkManager = ContentLinksManager.GetManager();
    var dynamicModuleManager = DynamicModuleManager.GetManager();
    var propertyName = "Image";
    var contentLink = new ContentLink()
    {
        ParentItemId = dynamicContent.Id,
        ParentItemProviderName = dynamicModuleManager.Provider.Name,
        ParentItemType = dynamicContent.GetType().ToString(),
        ComponentPropertyName = propertyName,
        ChildItemId = image.Id,
        ChildItemProviderName = contentLinkManager.Provider.Name,
        ChildItemType = "Telerik.Sitefinity.Libraries.Model.Image",
        ApplicationName = dynamicModuleManager.Provider.ApplicationName,
        ChildItemAdditionalInfo = image.GetThumbnailUrl()
    };
     
    dynamicContent.SetValue(propertyName, new[] { contentLink });

    where:
    dynamicContent is the dynamic item which you want to update
    propertyName is the name of the field to which you want to add the image
    image – the desired image

    I hope this information will help you. Please do not hesitate to contact me again if you have any questions or issues.

    Greetings,
    Georgi Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  4. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    08 Aug 2012
    Link to this post
    Hey Georgi...

    I'm looking at the DynamicContentExtensions class in the API right now (looking at the AddImage method the code samples tells us to call), and it pretty much mirrors (close to it) the code you pasted in here anyway...am I missing something?

    Steve
  5. Georgi Dimitrov
    Georgi Dimitrov avatar
    9 posts
    Registered:
    02 Jun 2017
    10 Aug 2012
    Link to this post
    Hi Steve,

    The difference between AddImage method and the provided code is that AddImage method doesn’t set ChildItemAdditionalInfo property which is used for the backend grid. That’s why the images don’t appear in the backend grid, sorry for the inconvenience.

    Kind regards,
    Georgi Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    10 Aug 2012
    Link to this post
    Yeah I updated the ticket on this one thanks :)

    ...so it's clearly a bug and will be fixed for 5.2 right?  (just 1 line of code which you already have I can't see why someone wouldnt just paste it in there now)
  7. Georgi Dimitrov
    Georgi Dimitrov avatar
    9 posts
    Registered:
    02 Jun 2017
    14 Aug 2012
    Link to this post
    Hi Steve,

    You are right this is a bug and it will be fixed for 5.2. Once again thank you for pointing this to us and sorry for the inconvenience.

    All the best,
    Georgi Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
7 posts, 0 answered