1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Saving Module Configuration Settings

Saving Module Configuration Settings

4 posts, 0 answered
  1. Garry
    Garry avatar
    52 posts
    Registered:
    04 Nov 2010
    17 Apr 2009
    Link to this post
    I have muddled my way through creating a module and I have finally gotten the Administration View setup and everything seems good to go, but now I have hit another road block in saving/getting my modules configuration settings.

    I believe I need to do something in the DefaultProvider, but I am not sure. I am over my head at this point and I am hoping that something clicks with a bit of help from you guys.

    If need be I can open a ticket and upload my entire module project. This one is the simpliest out of many I have to create so any other suggestions you have would greatly be appreciated!

    Thanks as always.

    SettingsView.cs
    using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using Telerik.Security.Permissions;  
    using GambitCreations.Sitefinity.WeatherModule.Resources;  
    using System.Web.UI.HtmlControls;  
    using System.Web.UI;  
    using System.ComponentModel;  
    using Telerik.Cms.Web.UI;  
    using GambitCreations.Sitefinity.WeatherModule.WebControls.Admin;  
    using System.Web.UI.WebControls;  
    using GambitCreations.Sitefinity.WeatherModule.Configuration;  
     
    namespace GambitCreations.Sitefinity.WeatherModule  
    {  
        ///<summary>  
        /// Provides user interface for managing settings of the Weather module.  
        ///</summary>  
        public class SettingsView<THost> : ViewModeControl<THost> where THost : ControlPanel  
        {
            #region Properties  
     
            /// <summary>  
            /// Gets the name of the embedded layout template. This property must be overridden to provide the path (key) to an embedded resource file.   
            /// </summary>  
            protected override string LayoutTemplateName  
            {  
                get 
                {  
                    return SettingsTemplateName;  
                }  
            }  
     
            /// <summary>  
            /// Gets or sets the layout template path.  
            /// </summary>  
            /// <value>The layout template path.</value>  
            [WebSysTemplate(SettingsTemplateName, "SettingsView_Template_Desc""/Sitefinity/"false"2009-03-15")]  
            public override string LayoutTemplatePath  
            {  
                get 
                {  
                    return layoutTemplatePath;  
                }  
                set 
                {  
                    layoutTemplatePath = value;  
                }  
            }
            #endregion  
     
            #region Control references  
     
            /// <summary>  
            /// Gets the License Key control.  
            /// </summary>  
            protected virtual ITextControl LicenseKey  
            {  
                get 
                {  
                    return base.Container.GetControl<ITextControl>("licenseKey"true);  
                }  
            }  
            /// <summary>  
            /// Gets the Provider ID control.  
            /// </summary>  
            protected virtual ITextControl PartnerID  
            {  
                get 
                {  
                    return base.Container.GetControl<ITextControl>("partnerID"true);  
                }  
            }  
            /// <summary>  
            /// Gets the Location ID control.  
            /// </summary>  
            protected virtual ITextControl LocationID  
            {  
                get 
                {  
                    return base.Container.GetControl<ITextControl>("locationID"true);  
                }  
            }  
            /// <summary>  
            /// Gets the save.  
            /// </summary>  
            /// <value>The save.</value>  
            protected virtual IButtonControl Save  
            {  
                get 
                {  
                    return base.Container.GetControl<IButtonControl>("save"true);  
                }  
            }  
            /// <summary>  
            /// Gets the message control for displaying information.  
            /// </summary>  
            protected virtual MessageControl MessageControl  
            {  
                get 
                {  
                    return base.Container.GetControl<MessageControl>("message"true);  
                }  
            }
            #endregion  
     
            #region Base overrides  
     
            /// <summary>  
            /// Initializes all controls instantiated in the layout container. This method is called at appropriate time for setting initial values and subscribing for events of layout controls.  
            /// </summary>  
            /// <param name="viewContainer">The control that will host the current view.</param>  
            protected override void InitializeControls(Control viewContainer)  
            {  
                // initialize manager for working with data  
                if (this.manager == null || this.manager.Provider.Name != this.providerName)  
                    this.manager = new WeatherManager(this.providerName);  
                  
                // define the behavior of save button  
                this.Save.Command += new CommandEventHandler(Button_Command);  
                this.Save.CommandName = "Save";  
            }  
     
            /// <summary>  
            /// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.  
            /// </summary>  
            /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>  
            protected override void OnPreRender(EventArgs e)  
            {  
                if (!string.IsNullOrEmpty(message) && Container != null)  
                {  
                    MessageControl.Message = message;  
                    MessageControl.Mode = messageMode;  
                }  
     
                base.OnPreRender(e);  
            }
            #endregion  
     
            #region Private methods  
            private void SaveSettings()  
            {  
                IWeather settings =  this.manager.GetSettings(this.settingsId);  
                settings.LicenseKey = LicenseKey.Text;  
                settings.LocationID = LocationID.Text;  
                settings.PartnerID = PartnerID.Text;  
                this.manager.SaveSettings(settings);  
                OnSaved(EventArgs.Empty);  
                MessageControl.Message = "Settings Successfully Saved!";  
            }  
            /// <summary>  
            /// Raises the <see cref="E:Saved"/> event.  
            /// </summary>  
            /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>  
            private void OnSaved(EventArgs e)  
            {  
                EventHandler<EventArgs> handler = (EventHandler<EventArgs>)base.Events[EventSavedKey];  
                if (handler != null)  
                    handler(this, e);  
            }
            #endregion  
     
            #region Event handlers  
            /// <summary>  
            /// Handles the Command event of the Button control.  
            /// </summary>  
            /// <param name="sender">The source of the event.</param>  
            /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>  
            private void Button_Command(object sender, CommandEventArgs e)  
            {  
                switch (e.CommandName)  
                {  
                    case "Save":  
                        SaveSettings();  
                        break;  
                }  
            }
            #endregion  
     
            #region Private fields  
     
            private string layoutTemplatePath;  
            private string message;  
            private MessageMode messageMode = MessageMode.Success;  
            private Guid settingsId;  
            private static object EventSavedKey = new object();  
            private WeatherManager manager;  
            private string providerName;        
            #endregion  
     
            #region Constants  
     
            public const string SettingsTemplateName = "GambitCreations.Sitefinity.WeatherModule.Resources.ControlTemplates.AdminControls.SettingsView.ascx";  
            public const string SettingsLocalizationFile = "GambitCreations.Sitefinity.WeatherModule.Resources.Messages.resx";
            #endregion  
        }  
    }  
     

    IWeather.cs
    using System;  
    using System.Collections.Generic;  
    using System.Text;  
     
    namespace GambitCreations.Sitefinity.WeatherModule  
    {  
        /// <summary>  
        /// Defines properties for Weather Module  
        /// </summary>  
        public interface IWeather  
        {  
            /// <summary>  
            /// Gets the ID.  
            /// </summary>  
            /// <value>The ID.</value>  
            Guid ID { get; }  
     
            /// <summary>  
            /// Gets or sets the first name.  
            /// </summary>  
            /// <value>The first name.</value>  
            string PartnerID { getset; }  
     
            /// <summary>  
            /// Gets or sets the last name.  
            /// </summary>  
            /// <value>The last name.</value>  
            string LicenseKey { getset; }  
     
            /// <summary>  
            /// Gets or sets the title.  
            /// </summary>  
            /// <value>The title.</value>  
            string LocationID { getset; }  
        }  

    DefaultProvider.cs
    using System;  
    using System.Collections;  
    using System.Collections.Generic;  
    using GambitCreations.Sitefinity.WeatherModule;  
    using System.Configuration;  
    using System.Web;  
    using System.Xml;  
    namespace GambitCreations.Sitefinity.WeatherModule.Providers  
    {  
        /// <summary>  
        /// This class represents the actual, concrete implementation of StarterKit provider.  
        /// </summary>  
        public class DefaultProvider : BaseModuleProvider  
        {  
            /// <summary>  
            /// Overriden. Creates new instance of IWeather object and returns it.  
            /// </summary>  
            /// <returns></returns>  
            public override IWeather CreateData()  
            {  
                // provide an actual implementation which will create data  
                // in some way and return the newly created IWeather object.  
                // The implementation is not mandated in any way. You can create  
                // text file here, or you can create new record in database with  
                // SqlCommands or you can use LINQ to SQL...  
     
                // in this sample we'll work with dummy data class  
                return new WeatherData();  
            }  
     
            /// <summary>  
            /// Overriden. Saves data.  
            /// </summary>  
            /// <param name="dataItem">IWeather object to be saved</param>  
            public override void SaveData(IWeather dataItem)  
            {  
                // provide an actual implementation which will save passed IWeather object  
                // in some way. The implementation is not mandated in any way. You can save  
                // text file here, or you can update the record in database with  
                // SqlCommands or you can use LINQ to SQL...  
     
                // in this sample we won't actually save data.  
            }  
     
            /// <summary>  
            /// Overriden. Reads data by primary key.  
            /// </summary>  
            /// <param name="key">Primay key of the data to be read</param>  
            /// <returns>IWeather object</returns>  
            public override IWeather ReadData(Guid key)  
            {  
                // provide an actual implementation which will find and return data  
                // in some way and return the found IWeather object.  
                // The implementation is not mandated in any way. You can read  
                // text file here, or you can read record from database with  
                // SqlCommands or you can use LINQ to SQL...  
     
                // in this sample we'll just create one simple instance of dummy data  
                // and return it bace  
     
                return null;  
            }  
     
            /// <summary>  
            /// Overriden. Reads all data.  
            /// </summary>  
            /// <returns>IList of IWeather objects.</returns>  
            public override IList ReadAllData()  
            {  
                // provide an actual implementation which will read all data  
                // in some way and return the list of IWeather objects.  
                // The implementation is not mandated in any way. You can read all  
                // text files from some folder here, or you can read all records from  
                // database with SqlCommands or you can use LINQ to SQL...  
     
                // in this sample we will just create a simple list with to IWeather objects  
                // and return it back  
     
                return null;  
            }  
     
            /// <summary>  
            /// Overriden. Deletes data.  
            /// </summary>  
            /// <param name="key">Primary key of the data object to be deleted.</param>  
            public override void DeleteData(Guid key)  
            {  
                // provide an actual implementation which will delete data  
                // in some way. The implementation is not mandated in any way. You can delete  
                // text file here, or you can delete a record from database with  
                // SqlCommands or you can use LINQ to SQL...  
     
                // in this sample we will not actually delete any data  
            }  
     
            public override IWeather GetSettings(Guid id)  
            {  
                WeatherData data = new WeatherData();  
                data.LicenseKey = "";  
                data.LocationID = "";  
                data.PartnerID = "";  
                return data;  
            }  
     
            /// <summary>  
            /// Creates new department  
            /// </summary>  
            public override IWeather CreateSettings()  
            {  
                return new WeatherData();  
            }  
     
            /// <summary>  
            /// Saves a department  
            /// </summary>  
            /// <param name="department">IDepartment object to be saved</param>  
            public override void SaveSettings(IWeather settings)  
            {  
                //WeatherData s = (settings)HttpContext.GetConfig("system.web/weatherModule");  
                //s.LicenseKey = settings.LicenseKey;  
                //s.PartnerID = settings.PartnerID;  
                //s.LocationID = settings.LocationID;  
            }  
     
            /// <summary>  
            /// Private class representing dummy data object. This class is used for demonstration  
            /// purposes. More commonly, provide would work with database, XML files or some  
            /// similar data storage...  
            /// </summary>  
            private class WeatherData : IWeather  
            {  
                public WeatherData()  
                {  
                }  
     
                public WeatherData(string _partnerid, string _licensekey, string _locationid)  
                {  
                    partnerid = _partnerid;  
                    licensekey = _licensekey;  
                    locationid = _locationid;  
                }  
     
                public Guid ID  
                {  
                    get { return id; }  
                    set { id = value; }  
                }  
     
                public string PartnerID  
                {  
                    get { return partnerid; }  
                    set { partnerid = value; }  
                }  
     
                public string LocationID  
                {  
                    get { return locationid; }  
                    set { locationid = value; }  
                }  
     
                public string LicenseKey  
                {  
                    get { return licensekey; }  
                    set { licensekey = value; }  
                }  
     
                private Guid id;  
                private string partnerid;  
                private string licensekey;  
                private string locationid;  
            }  
        }  
    }  
     
  2. Ivan
    Ivan avatar
    478 posts
    Registered:
    16 Jun 2015
    22 Apr 2009
    Link to this post
    Hi Garry Clark,

    first of all, sorry for the late reply.

    I don't see the implementation of your WeatherManager class in the code you have attached. Namely, the concept of provider model pattern is following:

    - you have the UI classes (in Sitefinity Views)
    - the UI classes instantiate the manager class with the proper provider name
    - the UI classes call methods on the manager class (e.g. SaveData)
    - since manager class knows which provider to use (the one you have instantiate it with), manager class will then call appropriate method on the appropriate provider
    - the provider will then do some actual work with the data

    The idea behind this pattern is that you can easily change the provider or data layer (e.g. xml, sql server, Amazon S3) without having to change any other part of your code.

    If you'd like to send us the whole module, you can open the ticket and we'll tie up the loose pieces for you and return you the project. All in all, it seems like you've got a pretty far with your module and there are only few details that need addressing. We'll try to provide you back with the solution ASAP.

    Sincerely yours,
    Ivan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Garry
    Garry avatar
    52 posts
    Registered:
    04 Nov 2010
    22 Apr 2009
    Link to this post
    Ivan,
    No worries on the lat response. I have been busy watching all the great Webinars on Sitefinity the last few days to even get back to this.

    After watching the webinar with Gabe Sumner and I believe yourself I decided to abandon the whole moduel and start from scratch rather than basing it off the pluggaable Contact example.

    I understand how the manager works with the provider in order to talk back to the DB, but what I am needing and wanting is still a way to CRUD the web.config using I guess a custom SectionHandler class. I saw one of these used in the Contacts module sample. I also believe I misspoke in my earlier post when I said I thought I would need to use the provider for this, but I believe I should now use the Manager class to talk to the SectionHandler class or should I just go straight to the SectionHandler class when working with the .config? As you can tell I am still confused about the whole custom SectionHanlder thing as I have always just used the ConfigurationManager to do such operations.

    Any help you can give me in sorting out my dilema would greatly be appreciated.

    Also keep up the great work on the Webinars they answered so many questions for me. I am much more comfortable with the 3.6 framework now.
  4. Ivan
    Ivan avatar
    478 posts
    Registered:
    16 Jun 2015
    22 Apr 2009
    Link to this post
    Hi Garry Clark,

    alright, I understand now. Well, when it comes to the configuration it's all the ASP.NET Configuration API. We have some classes that have standardized over the time to simplify the very common tasks we do, but there is no need for you to follow that same principle.

    ASP.NET Configuration API is very well documented and you can find abundant resources on it here: http://msdn.microsoft.com/en-us/library/ms178688.aspx

    If you are comfortable working with the ConfigurationManager, there is really no need for you to abandon that approach. Sitefinity does have some special requirements, when it comes to extensibility and reusability, so we do not go the simplest possible way always, but if it works for you, there is no reason why you shouldn't do it. Lots of times our approach could be an overkill if you are working on a one-time module.

    Anyhow, I am really glad that you've enjoyed the webinar. Gabe is really doing outstanding work there. Let us know if there is anything else we can do for you.

    Greetings,
    Ivan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Register for webinar
4 posts, 0 answered