More in this section

Forums / Developing with Sitefinity / Get current dynamiccontent item from server side code

Get current dynamiccontent item from server side code

4 posts, 0 answered
  1. Brian
    Brian avatar
    8 posts
    02 Aug 2014
    03 Sep 2014
    Link to this post

    Can someone please explain how to get the current dynamic content item from server side code on page_load?

    I have written a user control to display a custom gallery of sliding images. There are multiple content types in my dynamic module. The user control will sit as part of the masterpage template rather than on every page. On each page load I would like to fetch the current dynamiccontent item that is associated with the page and examine whether it has a property with the name 'Gallery'. If so I would then extract the images and render them via the usercontrol.



  2. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    18 Feb 2004
    03 Sep 2014 in reply to Brian
    Link to this post

    Hi Brian,

    I think the following would be a good approach:

    1. Determine which page you are on
    2. Get the value from the custom field on that specific page
    3. Use that value to get the right DynamicContent Item

    So to know which page you're on try something like this:

    var pageNode = SiteMap.CurrentNode as PageSiteNode;

    Then use the PageManager to get the PageNode and the custom field

    var manager = PageManager.GetManager(PageManager.GetDefaultProviderName());
    if (manager == null) return;
    var page = manager.GetPageNode(pageNode.Id);
    if (page == null) return;
    var customField = page.GetValue<string>("customField");
    if (customField.IsNullOrEmpty()) return;

    I'm retrieving a string type field here, but you could change it to get a DynamicContent field.



  3. Brian
    Brian avatar
    8 posts
    02 Aug 2014
    03 Sep 2014 in reply to Daniel Plomp
    Link to this post

    Daniel, thanks for the reply.

    Your code looks like it gets the current page node and then allows me to get the properties of the page. That's fine if I want something like the 'title' or 'owner' which are page fields but what I'm looking for the is the dynamic content type item that is rendered on the page via the dynamic content type widget.

    As an example, I have a 'regions' page. My site has a dynamicmodule with a content type 'regions'. I've placed the dynamic regions widget onto the regions page. The widget is set to initially show all published regions in a list and then redirect to the same page with /europe & /asia etc to show the full detail view of that single region.

    When visting the single region view of the 'regions' page for say 'Europe' I'd like to get the 'Europe' dynamiccontent item in the code behind of a user control on the page. I'd then examine the properties of the 'Europe' instance and do something with the property values such as rendering its associated images.

    Does that help explain the problem I have more clearly?



  4. Pavel Benov
    Pavel Benov avatar
    341 posts
    14 Mar 2016
    08 Sep 2014
    Link to this post
    Hi guys,

    @Brian - You can get the dynamic item from its url the following way:

    DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
    string redirectUrl;
    var dynamicItemType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.YourModule.YourModuleContentType");
    var item = dynamicModuleManager.Provider.GetItemFromUrl(dynamicItemType, itemUrl, true, out redirectUrl) as DynamicContent;

    Additionally to add up Daniel's suggestion in case you need to get the current page the following code would be more appropriate especially when used in Multisite:

    var pageSiteNode = SiteMapBase.GetActualCurrentNode();

    Pavel Benov
    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
4 posts, 0 answered