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

Forums / Developing with Sitefinity / How to get Related Media

How to get Related Media

7 posts, 0 answered
  1. Mirza Danish
    Mirza Danish avatar
    9 posts
    Registered:
    28 Apr 2014
    30 May 2014
    Link to this post

    Hi,

    How to get related media field from dynamic content module. I am using sitefinity 7.0 and use this method to get image but getting error, my code is below

    public static string GetImageSource(dynamic obj, string strImagePropertyName)
            {
                string strImagesource = null;
                ContentLink[] thumbNails = obj.GetType().GetProperty(strImagePropertyName).GetValue(obj, null);
                if (thumbNails != null && thumbNails.Length > 0)
                {
                    strImagesource = thumbNails[0].ChildItemAdditionalInfo;
                }
                return strImagesource;
            }

     , kindly tell me how to retrieve relatred meida item from dynamic content module .

  2. Vassil Vassilev
     Vassil Vassilev avatar
    308 posts
    Registered:
    21 Jan 2015
    31 May 2014
    Link to this post
    Hello Mirza,

    You could use the following code:

    using System;
    using System.Linq;
    using Telerik.Sitefinity.DynamicModules;
    using Telerik.Sitefinity.DynamicModules.Model;
    using Telerik.Sitefinity.Utilities.TypeConverters;
    using Telerik.Sitefinity.RelatedData;
    using Telerik.Sitefinity.Libraries.Model;
    using Telerik.Sitefinity.Modules.Libraries;
     
    namespace SitefinityWebApp
    {
        public partial class GetRelatedImages : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                var providerName = String.Empty;
                DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
                Type pressReleaseType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Pressreleases.PressRelease");
     
                var myCollection = dynamicModuleManager.GetDataItems(pressReleaseType).ToList();
     
                //getting some dynamic item
                DynamicContent pressReleaseItem = myCollection.First();
     
                //getting the image
                Image imageField = pressReleaseItem.GetRelatedItems<Image>("ImageField").SingleOrDefault();
     
                //getting the urls
                var relativeUrl = imageField.ResolveMediaUrl();
                var absolute = imageField.Url;
                var thumbnailUrl = imageField.ThumbnailUrl;
            }
        }
    }

    Hope this helps.

    Regards,
    Vassil Vassilev
    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
     
  3. kamii47
    kamii47 avatar
    71 posts
    Registered:
    02 Mar 2007
    31 May 2014 in reply to Vassil Vassilev
    Link to this post

    Ok .We were trying this mydynamicContent.GetRelatedItems method but it wasn't seems there.

    Looks like if we just put namespace
    using Telerik.Sitefinity.RelatedData;

    it will work.

    Thanks Vassil

  4. Mirza Danish
    Mirza Danish avatar
    9 posts
    Registered:
    28 Apr 2014
    27 Aug 2014
    Link to this post

    Thanks Kamii n Vassil 

  5. JT
    JT avatar
    1 posts
    Registered:
    05 May 2016
    13 Jun in reply to Vassil Vassilev
    Link to this post
    How would you do this for a news item instead of a press release?
  6. Dimitri Cools
    Dimitri Cools avatar
    80 posts
    Registered:
    29 Sep 2016
    14 Jun
    Link to this post
    Hi, 
    For retrieving the related image to a News item named MyNewsImage, you could do something like this:
    NewsManager newsManager = NewsManager.GetManager();
     
    var AllNews = newsManager.GetNewsItems().Where(newsItem => newsItem.Status == ContentLifecycleStatus.Live).ToList();
     
    foreach (var newsitem in AllNews)
     
    {
     
        Telerik.Sitefinity.Libraries.Model.Image relatedImage = newsitem.GetRelatedItems<Telerik.Sitefinity.Libraries.Model.Image>("MyNewsImage").SingleOrDefault();
     
        if (relatedImage != null)
     
        {
     
            var relativeUrl = relatedImage.ResolveMediaUrl();
     
            var absolute = relatedImage.Url;
     
            var thumbnailUrl = relatedImage.ThumbnailUrl;
     
         }
     
    }

    Some additional documentation:
    Content relations: Related data
    http://docs.sitefinity.com/content-relations-related-data


    Regards,
    Dimitri Cools
    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. 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 Dimitri Cools code.

    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);
                }
            }               
        }
    }
7 posts, 0 answered