Load properties from dynamic module on page
Hi, I have created a dynamic module Phone. It has a property named MyImage that is of image type.
I have a test page that has this module placed on it and I can successfully load the page with the API:
List<
PageNode
> pages = Telerik.Sitefinity.App.WorkWith().Pages().Where(x => x.Title == "Test with phone").Get().ToList();
PageNode myPage = pages[0];
List<
PageControl
> controls = myPage.GetPageData().Controls.ToList();
PageControl myControl = controls[0];
PageManager manager = PageManager.GetManager();
ObjectData data = manager.GetControl<
ObjectData
>(myControl.Id);
Control control = manager.LoadControl(data);
MvcWidgetProxy widget = control as MvcWidgetProxy;
if (null != widget)
var image = widget.Settings.Values["MyImage"];
[0]: [ListTemplateName, Phone]
[1]: [DetailTemplateName, Phone]
[2]: [DisableCanonicalUrlMetaTag, ]
[3]: [OpenInSamePage, True]
[4]: [DetailsPageId, 00000000-0000-0000-0000-000000000000]
[5]: [Model, Telerik.Sitefinity.Frontend.DynamicContent.Mvc.Models.DynamicContentModel]
Hi Ross,
Did you find a solution?
Thanks!
Yes, with some help from Laurent on the support team I was able to get the rest of the code to retrieve the detail.
From above, the Model can be used like so:
DynamicContentModel widgetModel = widget.Settings.Values[
"Model"
];
Type typeCateg = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.Phones.Phone"
);
// the dynamic type
string
ids = widgetModel.SerializedSelectedItemsIds;
// serialized form in string is like: ["xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"]
if
(
null
!= ids)
ids = ids.Replace(
"["
,
""
).Replace(
"]"
,
""
).Replace(
"\""
,
""
);
// sanitize to just guid data
Guid[] guids = ids.Split(
','
).Select(x => Guid.Parse(x.Trim())).ToArray();
if
(
null
!= guids && guids.Any())
Guid guid = guids[0];
// if there are multiple phones selected on the other page, we will use the first one
DynamicContent phone = dynMgr.GetDataItems(typeCateg).Where(x => x.Id == guid).FirstOrDefault();
if
(
null
!= phone)
return
phone.GetValue<Image>(
"MyImage"
);
// "MyImage" is the field name in Phone module
The one catch to this approach is that the widget on the page you are reading from must be configured to show "selected" items (on the Content tab when configuring widget). For our scenario this is valid but if you are showing items in the widget filtered by a tag or category (or showing all items) then you will need to modify the approach. If that's the case, try poking around in Telerik.Sitefinity.Frontend.DynamicContent.Mvc.Models.DynamicContentModel to see what properties/methods might help you determine the IDs of items the widget uses.
Hope that helps!
Noticed a bug where the image value was null when the page was live, but it was present when viewing a page in preview or edit mode. To correct that, use this line to query for the DynamicContent instead:
DynamicContent phone = dynMgr.GetDataItems(typeCateg).Where(x => x.OriginalContentId == guid && x.Status == ContentLifecycleStatus.Live).FirstOrDefault();