Pull single field value from a DynamicContent object of my Dynamic Module
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
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]"
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
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
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?
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;
@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!