+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

How to add additional logic into Sitefinity built in widgets

by Stanislav Velikov

It is often needed to extend some of the capabilities of Sitefinity built in widgets. Since the widgets implementation is in Sitefinity assemblies the code files are not directly accessible. To override the methods for the widgets it is needed to  inherit from the built in widget and override some of the methods. I will describe two approaches for doing so. One involves inheriting from the widget and creating new control with the modified logic. Another approach is to replace the built in widget with the modified one. The two approaches are interconnected.

First approach:
1. Create a .class file in your Sitefinity project solution then we must find out how to inherit one widget. For this example I will be inheriting Sitefinity login control. 
2. To find the needed  path that should be inherited (Telerik.Sitefinity.Web.UI.PublicControls.LoginControl) go to Administration->Settings->Advanced->Toolboxes->PageControls->Sections->Login->Tools->Login and find textbox: Control CLR Type or Virtual Path. In it you can take the path Telerik.Sitefinity.Web.UI.PublicControls.LoginControl as well as other properties that will not be needed fro this example.
3. In the .class file created in step 1 inherit from Login Control. The class should look like this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
  
namespace SitefinityWebApp.Custom
{
      public class Class1 : Telerik.Sitefinity.Web.UI.PublicControls.LoginControl
     {
        
    }
}

type override keyword to access a list of all methods that can be overridden. Make sure you are calling the base method when overriding to be sure the built in implementation for this method will also be included (base.LoginForm_Authenticate(sender, e);).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
   
namespace SitefinityWebApp.Custom
{
      public class Class1 : Telerik.Sitefinity.Web.UI.PublicControls.LoginControl
     {
        protected override void LoginForm_Authenticate(object sender, System.Web.UI.WebControls.AuthenticateEventArgs e)
            {
                base.LoginForm_Authenticate(sender, e);
            }
 
 
    }
}
4. Save the file and build Sitefinity project (if the project is website project build will not be needed).

5. Now register new widget that will be the modified login control.
For more information, see Registering a new widget in Sitefinity.

Go to Administration->Settings->Advanced->Toolboxes->Toolboxes->PageControls->Sections->Login->Tools and click create new button in the main content area. Register the control with
Control CLR Type or Virtual Path: SitefinityWebApp.CustomizedLoginControl
Name: CustomizedLoginControl

Title: CustomizedLoginControl

The customized control is now available in page edit screen under the Login section and the control is called CustomizedLoginControl.

Note some of the built in widgets that have separate views like news, blogs, events have two views. 
MasterListView this is the view that displays a list of all news, blogs, events items and sorts them.

DetailsView displays full contents of a single content item blog post, news item, event item.


Both views are using separate code file so the first approach with registering only one code file as a single control will not be of much use in this case. Refer to the second approach where you will be able to insert logic into a separate view (MasterListView or DetailsView). 

 
Second approach: 
With this approach step 1 is the same. To find the proper path to the desired MasterView or DetailsView go to

 Administration->Settings->Advanced->ContentView->Controls-> here a big node is expanded that list most of the backend controls. Go to NewsFrontend->Views and here you will see NewsFrontendList and NewsFrontendDetails. This naming is the same for all widgets that have two views (MasterList and Details).
To use different logic for NewsFrontendDetails click the menu item and find the textbox ViewType with content: Telerik.Sitefinity.Modules.News.Web.UI.DetailsSimpleView, this is the path from where the newsDetails code is taken and the path that we need to inherit.

Inherit  Telerik.Sitefinity.Modules.News.Web.UI.DetailsSimpleView in the class file created at step one and you will be able to overwrite methods from DetailsView.

To use the new DetailsView register it in the same box ViewType.SitefintiyWebApp.CustomDetailsViewClass. (the full path to the class file, namespace.class of the newly created class file.

namespace SitefinityWebApp.FolderWithTemplates
{
public class CustomNewsDetailsView : Telerik.Sitefinity.Modules.News.Web.UI.DetailsSimpleView
    {
Restart the application to observe the changes.

It is best you first test modifing the built in views on a test project first before making such modifications to the project.

 

3 comments

Leave a comment
  1. Alex Apr 25, 2012
    Great article. Looked everywhere to find this functionality. 
    Where do I find items for custom modules created using module builder? I found an item Toolboxes/PageControls/Sections/ContetnToolboxSection/Telerik.Sitefinity.DynamicTypes.Employees.Employee (in my case) which has DefaultMasterTemplateKey & DefaultDetailTemplateKey. How can I use this? There is also a node in ContentView but only for backend templates. I really need to add custom logic for custom modules. 
  2. Stan Jun 20, 2012
    For modules built with the Module Builder go to Administration->Settings->Advanced->DynamicModules and there will be an entry for your module (Telerik.Sitefinity.DynamicTypes.Employees.Employee) when you expand it you will see all of its views as described in the blog post
  3. ming Feb 20, 2013

    Toward the end of the article, you mentioned:

    >To use the new DetailsView register it in the same box ViewType.SitefintiyWebApp.CustomDetailsViewClass. (the full path to the class file, namespace.class of the newly created class file.

    What do you mean by this???

    This is what I have :

    namespace SitefinityWebApp.Controls
    {
         public class CustomNewsListView : Telerik.Sitefinity.Modules.News.Web.UI.MasterListView
         {
             protected override IQueryable<Telerik.Sitefinity.News.Model.NewsItem> GetItemsList(ref int? totalCount)

            {

                  IQueryable<Telerik.Sitefinity.News.Model.NewsItem> list = base.GetItemsList(ref totalCount);

                   // then add additional filter to my own classifications from Content-Type before returning the filtered list

            }
     

    Then I tried to use it as follows :

    <%@ Control Language="C#" Inherits="SitefinityWebApp.Controls.CustomNewsListView" %>

    But as soon as I saved my change, it will get

    Error parsing the template

                         

                         Ming

    Leave a comment