Programmatically change widget template.

Posted by Community Admin on 04-Aug-2018 16:07

Programmatically change widget template.

All Replies

Posted by Community Admin on 12-Apr-2012 00:00

How would one go about programmatically changing the template that widget is using?

Posted by Community Admin on 13-Apr-2012 00:00

Anyone out there?

Posted by Community Admin on 16-Apr-2012 00:00

Still looking for an answer.

Posted by Community Admin on 16-Apr-2012 00:00

Hi Erik,

The easiest way is to get the widget templates Id and creating a a usercontrol in your project that uses the same id. This give you full html/markup access to the template, as well as code-behind you can use to programmatically manipulate things. Below are the steps.

1. Get the widget's template id by dragging a widget out onto a page, go to it's "edit" properties and then "advanced". In advanced, you will see two fields, one called "DefaultDetailTemplateKey" and the other called "DefaultMasterTemplateKey". One corresponds to the detail template for the widget, the other for the master/list template. Copy the Guid in the field whose template you want to manipulate. Also go into the respective template editor and copy all the content in the default template for it and paste it off to a notepad file or something for reference later.

2. Now go into Visual Studio with the Sitefinity site loaded and create a folder off the root called "SfCtrlPresentation". 

3. Under this new folder, create a usercontrol (.ascx) file. Name the .ascx file as follows: "OpenAccessDataProvider,[paste template Guid here and delete any occurrences of "-" from the Guid string. The resulting filename should look as so: "OpenAccessDataProvider,1e7c45c2401c4de7b10b64fda20d92e2.ascx" (only with your guid replacing the example one here.)

4. Now open this newly created .ascx file and paste in the template markup you copied and saved off to the notepad file earlier. 

5. You need to do nothing more! Sitefinity will automatically pick up the template you've just created and ignore the one defined in the Widget's editor panel. You can now edit the markup as you wish and work in any logic, etc from its .vb or .cs code-behind file.


Posted by Community Admin on 16-Apr-2012 00:00

Thanks Craig, but I'm trying to do something a bit different. I have a widget that adds other widgets to the page based on some criteria. When I add these widgets to the page I want to set a specific template on them. For instance, I may be adding a NewsView to the page and I want to set it to use a custom widget template that I created for its detail view. I don't want to overwrite the existing templates as I use them in other scenarios. I just want to be able to define what template a widget uses when I add it to the page programmaticaly.

But don't get bogged down on the idea of me adding it to the page programaticaly. I would have the same issue if I had manually added the widget in the design view and then wanted to change the template on the fly.

Posted by Community Admin on 16-Apr-2012 00:00

I've been thinking about this over the weekend, so I'm sorry I haven't chimed in before now.

I haven't tried this, but I discovered some code in the Charity SDK sample initialization script (Global.asax) that demonstrates initializing an EventsView control and assigning it to a template:

var eventsControl = new EventsView();
eventsControl.MasterViewName = "EventsFrontendDateList";
eventsControl.ControlDefinition.GetDefaultDetailView().TemplateKey = SampleUtilities.GetControlTemplateKey(typeof(Telerik.Sitefinity.Modules.Events.Web.UI.Public.DetailsView), "Event Details with iCal");

I believe the key is the ControlDefinition.GetDefaultDetailView() which also exposes a TemplatePath property.

This should allow you to set the template programmatically when adding a new widget to the page.

This isn't a complete soluion, and I will continue to spend some time on this to get a working sample, but I hope that this helps to point you in the right direction in the meantime!

Posted by Community Admin on 16-Apr-2012 00:00

Thanks, that looks promising! I'll give it a shot and let you know how it turns out.

Posted by Community Admin on 17-Apr-2012 00:00

:) it works!

I was trying to set the template key like this (which didn't work):

newsControl.DetailViewDefinition.TemplateKey = "B2B1FDB7-F0A8-429A-8C64-ECC02EFC4ECF";

That one line where you do "ControlDefinition.GetDefaultDetailView().TemplateKey" was the key. I couldn't find the SampleUtilities class but I already knew the GUID so I used that. This solution also worked for dynamic content as follows:

var contentView = new DynamicContentView();
contentView.DynamicContentTypeName = "Telerik.Sitefinity.DynamicTypes.Model.Promos.Promo";
contentView.DetailViewDefinition.DataItemId = dataItemId;
contentView.ControlDefinition.GetDefaultDetailView().TemplateKey = "D0CC1EB5-DA98-4B44-8200-9C69AAF3A2D5";

All you need to do is get the type name of your module by debugging or looking in the database. Get the data item id for the item you want to display and then pull the GUID for the template key off of the name of the usercontrol.

Thanks!

This thread is closed