Get current dynamiccontent item from server side code
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.
Hi Brian,
I think the following would be a good approach:
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
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.
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;
var pageSiteNode = SiteMapBase.GetActualCurrentNode();