Quantcast
Channel: C# – Falafel Software Blog
Viewing all articles
Browse latest Browse all 54

Dynamic Content Detail Widget Templates in Sitefinity

$
0
0

Getting to Dynamic Content Data in Custom Code Behind

In a previous post, I went over how you can easily create a custom code-behind C# class for any Widget Template in Sitefinity. Give that post a quick once-over, as this post builds upon that one. For built-in Sitefinity content items, accessing the data is pretty straightforward (see the previously-linked post): You grab the container, iterate over the items (even in the case of a single detail item), and then work with the data to make customizations to the page. For Dynamic Content Detail Widget templates, however, things work differently. You can still get access to your data, but if you observe the content of a detail widget template for dynamic content, you’ll will find only a DynamicDetailContainer to work with. How do you access your item? Let’s find out!

Binding to DynamicDetailContainer’s DataBound Event

This first step is more-or-less identical to how we’ve done this before. We find our DynamicDetailContainer and assign a method to its DataBound event:

using System;
using System.Linq;
using System.Web.UI;
using Telerik.Sitefinity.DynamicModules.Model;
using Telerik.Sitefinity.DynamicModules.Web.UI.Frontend;
using Telerik.Sitefinity.Model;

namespace SitefinityWebApp.Custom
{
    public class CustomCodeBehind : UserControl
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            DynamicDetailContainer detailContainer = (DynamicDetailContainer)FindControl("detailContainer");
            if (detailContainer != null)
            {
                detailContainer.DataBound += DetailContainerDataBound;
            }
        }

        private void DetailContainerDataBound(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}

You can see how similar this is to the one we’ve created before! The only difference in the case of a Dynamic Content Detail Widget’s DynamicDetailContainer event is that we bind to a method whose parameters are of type “object” and “EventArgs.” Previously we bound to a method whose second parameter was “RadListViewItemEventArgs.”

Getting to Your DynamicContent Object

Now that we’re in our DataBound method, we need to reach out to our object we’re binding to. Since this container doesn’t iterate on DataBound, we have to access the entire collection of bound objects. For a Dynamic Content Detail Widget, this collection will always have just one object in play. The DynamicDetailContainer will possess this single-item collection, and the container itself is passed to the event via the “sender” parameter. Here’s what a complete example looks like:

using System;
using System.Linq;
using System.Web.UI;
using Telerik.Sitefinity.DynamicModules.Model;
using Telerik.Sitefinity.DynamicModules.Web.UI.Frontend;
using Telerik.Sitefinity.Model;

namespace SitefinityWebApp.Custom
{
    public class CustomCodeBehind : UserControl
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            DynamicDetailContainer detailContainer = (DynamicDetailContainer)FindControl("detailContainer");
            if (detailContainer != null)
            {
                detailContainer.DataBound += DetailContainerDataBound;
            }
        }

        private void DetailContainerDataBound(object sender, EventArgs e)
        {
            DynamicDetailContainer detailContainer = (DynamicDetailContainer)sender;
            DynamicContent[] dynamicItems = (DynamicContent[])detailContainer.DataSource;
            DynamicContent dynamicItem = dynamicItems.FirstOrDefault();
            if (dynamicItem == null)
            {
                return;
            }
            
            // Perform customizations with dynamicItem here.
        }
    }
}

We first cast “sender” to the type DynamicDetailContainer. Dynamic Content Detail Widget’s DetailContainer DataBound event always passes this. This object has a “DataSource” property where the collection of DynamicContent items, stored as an array, can be found. From there it’s a simple matter of LINQing out our single object, verifying we got it, then performing whatever customizations we desire based off of our bound object.

Working with your DynamicContent Object

As expected, the Dynamic Content Detail Widget’s DetailContainer works with the live, published version of the item. It would be really weird if it didn’t! Sitefinity binds this item to the container to show the live, published data, and now the power’s in your hands as well. You can work with this item the way you would work with DynamicContent items in any other programming context: Fetch custom fields, get related data, etc. You could technically re-fetch it too, using the given ID, but since the container already has the object for you that would be redundant.

And that’s it! This functionality took me a little digging to find, but now you can find it easily here. Working with Dynamic Content Detail Widget code-behinds is as easy as working with any widget template code-behind, once you know how to get to the item. Now you do!

The post Dynamic Content Detail Widget Templates in Sitefinity appeared first on Falafel Software Blog.


Viewing all articles
Browse latest Browse all 54

Trending Articles