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

Forums / Developing with Sitefinity / How to get imageurl in sitefinity 7.0

How to get imageurl in sitefinity 7.0

10 posts, 0 answered
  1. NARENDRA
    NARENDRA avatar
    3 posts
    Registered:
    03 Jun 2014
    16 Jul 2014
    Link to this post

    Hi All,

    I'm Using sitefinity 7.0 , I'm Trying to Get Image Url's but its Getting Error.i followed this link

    I'm Getting Below Error Message:

    Unable to cast object of type 'System.Collections.Generic.List`1[Telerik.Sitefinity.Model.IDataItem]' to type 'Telerik.Sitefinity.Model.ContentLinks.ContentLink[]'. 

     working in sitefinity 6.2. but not in 7.0.

    Please help me.

  2. Josh
    Josh avatar
    6 posts
    Registered:
    13 Jul 2012
    16 Jul 2014
    Link to this post

    I'm running into the same issue trying to retrieve a document URL.  

    I have a custom module that was created through the CMS.  Fields that are Short or Long text are easy enough to retrieve using:
    planDetailItem.GetValue("title").ToString();

    where planDetailItem is the DynamicContent object.

    When attempting to access the document object, it is returned as an object that cannot be cast using the methods I've found on the Telerik site. 

    Error is:

    System.InvalidCastException was unhandled by user code
      HResult=-2147467262
      Message=Unable to cast object of type 'System.Collections.Generic.List`1[Telerik.Sitefinity.Model.IDataItem]' to type 'Telerik.Sitefinity.Model.ContentLinks.ContentLink[]'.
      Source=Sitefinity

     This is happening in sitefinity 7, using Visual Studio 2013.

  3. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    16 Jul 2014 in reply to Josh
    Link to this post

    Hey guys,

    Maybe because it uses RelatedData, you should do it like this:

    var relatedImages = dynamicItem.GetRelatedItems<Image>("ImageField");

    I'm not sure, since I'm not behind my dev machine.

    Kind regards,
    Daniel

  4. Josh
    Josh avatar
    6 posts
    Registered:
    13 Jul 2012
    16 Jul 2014
    Link to this post

    I was able to get to the doc by casting it to a list of DataItem, as follows, then using the itemID to get the document to work with.  Should be a similar process with images.

                    List<Telerik.Sitefinity.Model.IDataItem> testList = new List<IDataItem>();

                    testList = (List<Telerik.Sitefinity.Model.IDataItem>)dynamicContentItem.GetValue("Document");
                    if (testList.Count > 0)
                    {
                        LibrariesManager librariesManager = LibrariesManager.GetManager();
                        Document document = librariesManager.GetDocuments().Where(d => d.Id == testList[0].Id).FirstOrDefault();
         
                    }

  5. David Rhodes
    David Rhodes avatar
    42 posts
    Registered:
    24 May 2005
    17 Jul 2014 in reply to Josh
    Link to this post

    I use the following, this is a related data image with max 1 allowed

    Telerik.Sitefinity.Libraries.Model.Image image = (Telerik.Sitefinity.Libraries.Model.Image)dataItem.GetValue("BackgroundImage");
    if (image != null)
     {
     listItem.Attributes.Add("data-backgroundimage", image.MediaUrl);
     }

  6. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    21 Jul 2014
    Link to this post
    Hi,

    There are several ways to obtain an image Url. However, you should first get the Image object. You can achieve this by using the extension methods GetValue or GetValue<T> from the Telerik.Sitefinity.Model namespace, for instance:
    Copy Code
    var img = dataItem.GetValue<Telerik.Sitefinity.Libraries.Model.Image>("MyImageField");

    If you are using Related Media field for the image use the GetRelatedItems method or GetRelatedItems<T> method. You should use the Telerik.Sitefinity.Libraries.Model.Image as a type for Images, as Daniel suggested. The GetRelatedItems method is part of the Telerik.Sitefinity.RelatedData namespace. The you should use the queried data depending on whether the field holds a single Image or a collection.
    var relatedImages = item.GetRelatedItems<Telerik.Sitefinity.Libraries.Model.Image>("ImageField");

    Then using the Image you could get its Url:
    Copy Code
    img.Url;

    Default location Url
    Copy Code
    SystemManager.GetContentLocationService().GetItemDefaultLocation(img).ItemAbsoluteUrl;

    or Resolve its Url and get full UrlPath:
    Copy Code
    UrlPath.ResolveUrl(DataResolver.Resolve(img, "URL", null), true);

    Regards,
    Nikola Zagorchev
    Telerik
     
    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 Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  7. Arekayee
    Arekayee avatar
    3 posts
    Registered:
    04 Apr 2014
    20 Aug 2014 in reply to Nikola Zagorchev
    Link to this post

    Hi guys, I apologize as I'm new(er) to Sitefinity and even moreso to the use of RelatedData.

    Can you tell me if I can perform the same function through a widget template.  I have a module (Events) which contains a RelatedData field to another module (Sponsors).  The sponsors module has a RelatedMedia field containing images (logos).  When I drag the associated widget for Events onto my page I would like to be able to display the associated images from the Sponsors module. Unfortunately, I dont have the luxury of a code behind on this one.

    Also, I've found that there are many methods for grabbing images but I'm not able to see (yet) a pattern for when to use which method.  Is there a good reference available for this?

     Thanks and best regards,

  8. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    25 Aug 2014
    Link to this post
    Hello Arekayee,

    When adding the related data field, you have the option to choose how the items will be shown in the front end - using simple links or the items widget. If you choose to show the items using the widget, and have set up the related data items widget to show the corresponding module related images, they should show in the first module widget. For instance: Events -> Sponsors (show using widget) -> in Sponsors widget show the Related Images using the autogenerated controls from the right toolbox -> fields -> your related media field.
    You can always add a 'codebehind' to a default Sitefinity template using the Inherits property of the Control:
    <%@ Control Language="C#" Inherits="SitefinityWebApp.Extender" %>

    The extender 'codebehind' class should inherit from UserControl. You can attach to any control event from the control's lifecycle, as well as, define static helper methods to be called from the template.
    namespace SitefinityWebApp
    {
        public partial class Extender : UserControl
        {
    // custom logic here
        }
    }
    Hope this is useful.

    Regards,
    Nikola Zagorchev
    Telerik
     
    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 Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  9. Murli
    Murli avatar
    4 posts
    Registered:
    18 Nov 2015
    22 Jun
    Link to this post

    Hi,

    After a week struggle finally i have resolved my issue with guideline of one of the user.
    I have modified some code at my end. may be its work for another users.

    Code is :

    Front end changes are

    <asp:Repeater ID="rpt_custom" runat="server" OnItemDataBound="rpt_custom_ItemDataBound" >
        <ItemTemplate>
            <div class="mid-left">
                <asp:Image ID="MyDataImage" Height="150" Width="150" runat="server" AlternateText="Image not found" />
                <h1><%#Eval("Headline") %></h1>
                <br />
                <asp:Panel ID="imagePanel" CssClass="ImgContainer" runat="server" />
            </div>
        </ItemTemplate>
    </asp:Repeater>

    Code Behind changes are

    using Telerik.Sitefinity.DynamicModules;
    using Telerik.Sitefinity.Modules.Libraries;
    using Telerik.Sitefinity.Security;
    using Telerik.Sitefinity.RelatedData;
    using Telerik.Sitefinity.Model.ContentLinks;
    using System.ComponentModel;
    using Telerik.Sitefinity.Libraries.Model;
     
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
     
    public void BindData()
    {
        rpt_custom.DataSource = RetrieveCollectionOfDataThumbLists();
        rpt_custom.DataBind();
    }
     
    public IQueryable<DynamicContent> RetrieveCollectionOfDataThumbLists()
    {
        // Set the provider name for the DynamicModuleManager here. All available providers are listed in
        // Administration -> Settings -> Advanced -> DynamicModules -> Providers
        var providerName = String.Empty;
        DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
        Type datathumblistType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.DataThumbList.Datathumblist");
     
        //To Insert new record into the database
        //CreateDatathumblistItem(dynamicModuleManager, datathumblistType);
     
        // This is how we get the collection of DataThumbList items
        // var myCollection = dynamicModuleManager.GetDataItems(datathumblistType);
     
        var myCollection = dynamicModuleManager.GetDataItems(datathumblistType)
            .Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true);
     
        // At this point myCollection contains the items from type datathumblistType
        return myCollection;
    }
     
    public void rpt_custom_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //Telerik.Sitefinity.Libraries.Model.Image newsImage = (Telerik.Sitefinity.Libraries.Model.Image)e.Item.DataItem.GetRelatedItems<Telerik.Sitefinity.Libraries.Model.Image>("DataImage").First();
            //var imageControl = (System.Web.UI.WebControls.Image)e.Item.FindControl("MyDataImage");
            //if (imageControl != null)
            //{
            //    // i have just added 2 times this to show you that i have tried both but non of them working
            //    imageControl.ImageUrl = newsImage.MediaUrl;
            //}
     
            IList<Telerik.Sitefinity.Libraries.Model.Image> newsImageArray = e.Item.DataItem.GetRelatedItems<Telerik.Sitefinity.Libraries.Model.Image>("DataImage").ToList();
            var imagePanel = (System.Web.UI.WebControls.Panel)e.Item.FindControl("imagePanel");
            if (imagePanel != null)
            {
                foreach (Telerik.Sitefinity.Libraries.Model.Image im in newsImageArray)
                {
                    System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
                    img.Height = img.Width = 150;
                    img.ImageUrl = im.MediaUrl;
     
                    imagePanel.Controls.Add(img);
                }
            }              
        }
    }

     

    Comments welcome

    Regards,

    D.Murli

    www.murlid05.blogspot.com

  10. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    22 Jun
    Link to this post
    Hi Murli,

    Thank you for sharing with the community.

    Regards,
    Nikola Zagorchev
    Telerik
     
    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 Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
10 posts, 0 answered