Get current dynamiccontent item from server side code

Posted by Community Admin on 04-Aug-2018 13:35

Get current dynamiccontent item from server side code

All Replies

Posted by Community Admin on 03-Sep-2014 00:00

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.

Thanks,

Brian.

Posted by Community Admin on 03-Sep-2014 00:00

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.

Best,
Daniel

 

Posted by Community Admin on 03-Sep-2014 00:00

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?

Thanks,

Brian.

Posted by Community Admin on 08-Sep-2014 00:00

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();

Regards,
Pavel Benov
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
 

This thread is closed