More in this section

Forums / Developing with Sitefinity / Load properties from dynamic module on page

Load properties from dynamic module on page

4 posts, 1 answered
  1. Ross
    Ross avatar
    0 posts
    Registered:
    03 Mar 2016
    11 Jan
    Link to this post
    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];

    Now I am trying to load the widget details:

    List<PageControl> controls = myPage.GetPageData().Controls.ToList();

    I am able to see there is one control on the page and retrieve it:

    PageControl myControl = controls[0];

    At this point I am stuck on how to get the property data from the object (the image that the content item references). I have seen a few posts on the matter:
    http://knowledgebase.progress.com/articles/Article/reading-and-updating-control-properties-on-a-page
    http://knowledgebase.progress.com/articles/Article/Place-a-MVC-widget-on-page-and-set-its-properties-using-the-API

    They both mention casting the object. If I try and cast the control directly I get an exception. So I am trying the following code to load the control by ID:
     
    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"];
    }

    However I am not getting the data and the Settings.Values throws an error because there is no property "Values" on it. If I output the Settings, I see only:

    [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]}

    If I dive further into the Model property I still see no relevant information.

    I am stuck and would appreciate any advice.  Thank you.
  2. Eleonora
    Eleonora avatar
    0 posts
    Registered:
    17 Sep 2015
    24 Jan
    Link to this post

    Hi Ross,

     

    Did you find a solution?

     

    Thanks!

  3. Ross
    Ross avatar
    0 posts
    Registered:
    03 Mar 2016
    25 Jan
    Link to this post

    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!

    Answered
  4. Ross
    Ross avatar
    0 posts
    Registered:
    03 Mar 2016
    02 Feb
    Link to this post

    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();
4 posts, 1 answered