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

Forums / Developing with Sitefinity / Pull single field value from a DynamicContent object of my Dynamic Module

Pull single field value from a DynamicContent object of my Dynamic Module

7 posts, 0 answered
  1. Craig
    Craig avatar
    82 posts
    Registered:
    07 Apr 2009
    28 Jan 2013
    Link to this post

    I have a code-behind setup on my detail widget template of a Dynamic Module.  The widget template displays a single PRODUCT content type.

    I was able to pull back a single product on the Page_Load event of my code behind using the following code below.

    var dc = (DynamicDetailContainer)this.FindControl("detailContainer");
    var p = ((Telerik.Sitefinity.DynamicModules.Model.DynamicContent[])dc.DataSource)[0];

    When I examine my variable "p" it is of type DynamicContent.  At this point I want to pull the individual field values of this content type, but after reviewing the API and am not sure how to do pull a single value.

    HOW DO YOU PULL A SINGLE FIELD VALUE FROM A DYNAMICCONTENT OBJECT?  For example, the TITLE of the specific piece of content.

    If anyone has any ideas please let me know.


    Thanks,
    Craig

  2. Victor
    Victor avatar
    94 posts
    Registered:
    15 Jun 2011
    28 Jan 2013 in reply to Craig
    Link to this post
    You would use the GetValue<T>  method to get the values.

    What I've found helpful is the built-in documentation for DynamicContent.  Goto:
    Settings>>Module Builder>>[MODULE]  Then click the link for "Code reference for [module]"
  3. Craig
    Craig avatar
    82 posts
    Registered:
    07 Apr 2009
    28 Jan 2013 in reply to Craig
    Link to this post

    I got my answer...

    I threw my post up on the Sitefinity Devs Google+ site an go an answer in a few minutes.  Really starting to like Google+.

    Anyways, I was missing a namespace (Telerik.Sitefinity.Model) in my code to expose the .SetValue() and .GetValue() as they are extension methods on the DynamicContent object.  Thanks Daniel Plomp.  :-)

    Enjoy!

    Craig

  4. Craig
    Craig avatar
    82 posts
    Registered:
    07 Apr 2009
    28 Jan 2013 in reply to Victor
    Link to this post

    Victor,

    Thanks for the reply.  

    I have reviewed the same code reference section under the Module Builder section.  I found out I was missing a namespace in my code.  Should be good to go now.  Thanks.


    Craig

  5. Brian
    Brian avatar
    8 posts
    Registered:
    02 Aug 2014
    03 Sep 2014 in reply to Craig
    Link to this post

    Did anyone ever find any other way of getting the current dynamic content instance from the current page_load code behind other than searching for a control that its bound to?

  6. Ivan D. Dimitrov
    Ivan D. Dimitrov avatar
    310 posts
    Registered:
    26 Mar 2015
    08 Sep 2014
    Link to this post
    Hi all,

    @Brian

    I believe my colleague Pavel Benov posted a reply for this question in this thread. Let me elaborate on his answer here. The method suggested is the GetItemFromUrl one. The only tricky thing you need to watch out for is that the method expects the URL of the dynamic item as set in the database, which is the item title set to lower with dashes instead of spaces and preceded by an forward slash. Therefore you will need to trim the page URL and the domain from the absolute URL. Here is some sample code on how to achieve this:
    var appUrl = VirtualPathUtility.ToAbsolute("~/");
     
             // remove the app path (exclude the last slash)
             var relativeUrl = HttpContext.Current.Request.Url.AbsolutePath.Remove(0, appUrl.Length - 1);
            // trim the page url - 8 characters in this case - testpage
             string itemUrl = relativeUrl.Remove(0, 8);
             string redirectUrl;
             DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
             Type pressReleaseType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Pressreleases.PressRelease");
             Type newsreleasesType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Pressreleases.Newsreleases");
            //pass the ItemUrl to the method
             var parentItem = dynamicModuleManager.Provider.GetItemFromUrl(pressReleaseType, itemUrl, true, out redirectUrl) as DynamicContent;


    Regards,
    Ivan D. Dimitrov
    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. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    03 Nov in reply to Craig
    Link to this post
    @Craig - you have no idea how many hours this has cost me and then I finally stumble upon your post about the missing namespace! Thank you! 
7 posts, 0 answered