Running server-side code on News Widget template

Posted by Community Admin on 03-Aug-2018 17:01

Running server-side code on News Widget template

All Replies

Posted by Community Admin on 01-Sep-2011 00:00

Hi,
I am trying to accomplish the example laid out in this post:
www.sitefinity.com/.../running_server-side_code_in_sitefinity_widget_templates.aspx

The only difference is that I am trying to do this with the news widget instead of the events widget.  I went in to the single item news details template and added the code supplied in the example:
<script runat="server"> protected void Page_Load(object sender, EventArgs e)
   
        var view = this.Parent.Parent as Telerik.Sitefinity.Modules.Events.Web.UI.Public.DetailsView;
        if (view == null) return;

        var detailItem = view.Host.DetailItem;
        if (detailItem == null) return;

        Page.Title = detailItem.Title;
   
</script>

However, there is no change to the title meta tag on the page- it still displays the title of the parent page.  Should this code not work for the News widget the same?  or does it need to be altered?  or does it need to be placed in a specific place within the template's code?  (I tried placing it at the beginning, the end, and the middle to no avail).  I am not a coder

thanks!

Posted by Community Admin on 06-Sep-2011 00:00

Hi Ryan,

Please excuse us for the inconvenience. For security reasons we are stripping server side code from the widget templates created/edited from Sitefninty backend since Sitefinity 4.2 official release. However, it's possible to map an external template which implements the desired functionality in the code-behind. To do so, you can use the default template applied to that particular view, and create the external template in your project. Once you've built it you can go to the widget's advanced settings -> ControlDefinition->Views and select the desired view. In the dialog there should be a TemplatePath property, wehre you should specify the application relative path to the external template in your project. Please make sure to delete the value in the TemplateKey field, as otherwise the embedded template will be loaded. Restart the application and your external temp[late will be loaded for that particular widget. If you wish to map the template globally for all instances of the widget, you can go to Administration -> Controls -> ViewMap and create a new entry there. In the HostType field enter the desired VieType (example: for News widget, details View, it will be Telerik.Sitefinity.Modules.News.Web.UI.DetailsSimpleView). In LayoutTemplate path, place the application relative path to the external template in your project. Save the changes and restart the application, an you will have your template mapped globally for all instances of the widget.

All the best,
Boyan Barnev
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Posted by Community Admin on 07-Sep-2011 00:00

For more examples on different ways to use external templates for your controls, take a look at these blog posts:

Customizing Sitefinity 4 Controls with the ViewMap
Mapping External Templates for Sitefinity 4 Widgets

Hope this is helpful!

This thread is closed