How to get Related Media - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

How to get Related Media

  • How to get Related Media
  • 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 .

  • 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
     
  • 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

  • Thanks Kamii n Vassil 

  • How would you do this for a news item instead of a press release?
  • 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
     
  • 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);
                
                           
        
  • Does anyone know if Telerik.Sitefinity.RelatedData existed in Sitefinity 5.4? 

    I'm following this article to create a parallax slider widget but it won't build because it can't find GetRelatedItems and Telerik.Sitefinity.RelatedData. 

    All fundamental mechanics seem to be there in 5.4 in terms of creating Dynamic Modules with related data (images), but I'm not sure about the assemblies. 

    If it didn't exist, does anyone have a suggested workaround since upgrading to the latest version of Sitefinity is not an option.