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!
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:
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.”
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:
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.
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!