LinqPrep: Programmatically adding Linq to SQL keys to the web.config of your DotNetNuke 4 site

Download module: LinqPrep_01.00.00_Install.zip

Note: Before running this program you will want to backup your web.config file (in the root of your DotNetNuke site). Also, Linq To SQL does not support the DotNetNuke object qualifier setting.

DotNetNuke 5 has an option in the administration settings that will allow the site to run a module that uses Linq to SQL. However, there are no plans to provide this functionality in DotNetNuke 4.

This program will allow you to automatically add the needed keys to the web.config file of the DotNetNuke website to allow a module that uses Linq to SQL to run.

Installing LinqPrep

Ensure that ASP.NET 3.5 (or higher) is installed on the server.

Log in as SuperUser (the Host account) and from the Host menu, select Module Definitions.

Select the module installation zip file using the Browse button (download from here: LinqPrep_01.00.00_Install.zip) , and click the Install New Module link.

After the module installs, click the Return link.

Navigate to a page in the DotNetNuke site and select LinqPrep from the Module drop-down. Click the Add link.

The module will indicate if ASP.NET 2.0 and 3.5 are installed. It will also indicate if there are any keys that need to be added to the web.config file.

If there are, click the Update Web.Config button.

The program will update the web.config file and indicate the DotNetNuke site is ready to run Linq to SQL.

If there are Problems

If the program does not work, you can update the web.config file manually:

Change: the <system.codedom> section to:

<system.codedom>
<compilers>
<compiler language="vb;vbs;visualbasic;vbscript" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" extension=".vb" warningLevel="4">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="OptionInfer" value="true"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>

Change: the <assemblies> section to:

<assemblies>
<add assembly="Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Management, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies> 

Change: the <controls> section to:

<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</controls>

 
Note: Help with the Parser Error: Child Notes Not Allowed

The Code

The following is the source code for the program:

//
// DotNetNuke® - http://www.dotnetnuke.com
// Copyright (c) 2002-2008
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//
 
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Security;
using Microsoft.Win32;
using System.Globalization;
using System.Xml;
using System.Web.Configuration;
using System.Configuration;
using System.IO;
 
namespace LinqPrep
{
    public partial class View : DotNetNuke.Entities.Modules.PortalModuleBase
    {
        const string Netfx35RegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v3.5";
 
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                lblCurrentVersion.Text = String.Format("Current version of ASP.NET: {0}.{1}.{2}", 
                    Environment.Version.Major.ToString(), Environment.Version.Minor.ToString(), Environment.Version.Build.ToString());
                lblASPNET35Installed.Text = String.Format("ASP.NET 3.5 {0} installed.", 
                    (IsNetfx35Installed()) ? "is " : "is not ");
 
                if (IsNetfx35Installed())
                {
                    Panel1.Visible = true;
                    ShowStatus();
                }
                else
                {
                    lblCanContinue.Text = "You must install asp.net 3.5 (or higher) from http://www.asp.net to continue.";
                }
 
            }
            catch (Exception ex)
            {
                lblCurrentVersion.Text = "Unable to detect ASP.NET version or create web.config keys";
                lblASPNET35Installed.Text = ex.Message;
            }
        }
 
        #region ShowStatus
        private void ShowStatus()
        {
            if ((MissingAssemblies() || MissingSystemCodeDom() || MissingControls()))
            {
                lblAssemblies.Text = "The web.config file is missing keys";
                btnUpdateWebConfig.Visible = true;
            }
            else
            {
                lblAssemblies.Text = "The site is configured to run Linq to SQL";
                btnUpdateWebConfig.Visible = false;
            }
        } 
        #endregion
 
        #region GetRegistryValue
        private static bool GetRegistryValue<T>(RegistryHive hive, string key, string value, 
            RegistryValueKind kind, out T data)
        {
            bool success = false;
            data = default(T);
 
            using (RegistryKey baseKey = RegistryKey.OpenRemoteBaseKey(hive, String.Empty))
            {
                if (baseKey != null)
                {
                    using (RegistryKey registryKey = baseKey.OpenSubKey(key, RegistryKeyPermissionCheck.ReadSubTree))
                    {
                        if (registryKey != null)
                        {
                            // If the key was opened, try to retrieve the value.
                            RegistryValueKind kindFound = registryKey.GetValueKind(value);
                            if (kindFound == kind)
                            {
                                object regValue = registryKey.GetValue(value, null);
                                if (regValue != null)
                                {
                                    data = (T)Convert.ChangeType(regValue, typeof(T), CultureInfo.InvariantCulture);
                                    success = true;
                                }
                            }
                        }
                    }
                }
            }
            return success;
        }
        #endregion
 
        #region IsNetfx35Installed
        private static bool IsNetfx35Installed()
        {
            // from: http://www.codeproject.com/KB/cs/frameworkversiondetection.aspx
            bool found = false;
            int regValue = 0;
 
            if (GetRegistryValue(RegistryHive.LocalMachine, Netfx35RegKeyName, "Install", 
                RegistryValueKind.DWord, out regValue))
            {
                if (regValue == 1)
                {
                    found = true;
                }
            }
 
            return found;
        }
        #endregion
 
        #region MissingSystemCodeDom
        bool MissingSystemCodeDom()
        {
            bool boolCodeDomMissing = false;
 
            System.Configuration.Configuration rootWebConfig = 
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            DefaultSection DefaultSection = (DefaultSection)rootWebConfig.GetSection("system.codedom");
            SectionInformation SectionInformation = (SectionInformation)DefaultSection.SectionInformation;
 
            string strSystemCodeDom = SectionInformation.GetRawXml();
 
            if (!strSystemCodeDom.ToLower().Contains("vbcodeprovider"))
            {
                boolCodeDomMissing = true;
            }
 
            if (!strSystemCodeDom.ToLower().Contains("csharpcodeprovider"))
            {
                boolCodeDomMissing = true;
            }
 
            if (!strSystemCodeDom.ToLower().Contains("v3.5"))
            {
                boolCodeDomMissing = true;
            }
 
            return boolCodeDomMissing;
        }
        #endregion
 
        #region MissingAssemblies
        bool MissingAssemblies()
        {
            bool boolMissingAssemblies = false;
            System.Configuration.Configuration rootWebConfig = 
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            SystemWebSectionGroup SystemWebSectionGroup = 
                (SystemWebSectionGroup)rootWebConfig.GetSectionGroup("system.web");
            AssemblyCollection AssemblyCollection = SystemWebSectionGroup.Compilation.Assemblies;
 
            if (SearchForAssembly("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089", 
                AssemblyCollection)
                == false) { boolMissingAssemblies = true; }
 
            if (SearchForAssembly("System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089", 
                AssemblyCollection)
                == false) { boolMissingAssemblies = true; }
 
            if (SearchForAssembly("System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089", 
                AssemblyCollection)
                == false) { boolMissingAssemblies = true; }
 
            if (SearchForAssembly("System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", 
                AssemblyCollection)
                == false) { boolMissingAssemblies = true; }
 
            return boolMissingAssemblies;
        }
        #endregion
 
        #region SearchForAssembly
        bool SearchForAssembly(string AssemblyName, AssemblyCollection ColAssemblyCollection)
        {
            bool boolAssemblyFound = false;
            foreach (AssemblyInfo AssemblyInfo in ColAssemblyCollection)
            {
                if (AssemblyInfo.Assembly.ToLower() == AssemblyName.ToLower())
                {
                    boolAssemblyFound = true;
                }
            }
 
            return boolAssemblyFound;
        }
        #endregion
 
        #region MissingControls
        bool MissingControls()
        {
            bool boolMissingControls = false;
            System.Configuration.Configuration rootWebConfig = 
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            SystemWebSectionGroup SystemWebSectionGroup = 
                (SystemWebSectionGroup)rootWebConfig.GetSectionGroup("system.web");
            TagPrefixCollection TagPrefixCollection = SystemWebSectionGroup.Pages.Controls;
 
            if (SearchForControl("System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35", 
                TagPrefixCollection)
                == false) { boolMissingControls = true; }
 
            return boolMissingControls;
        }
        #endregion
 
        #region SearchForControl
        bool SearchForControl(string AssemblyName, TagPrefixCollection ColTagPrefixCollection)
        {
            bool boolControlFound = false;
            foreach (TagPrefixInfo TagPrefixInfo in ColTagPrefixCollection)
            {
                if (TagPrefixInfo.Assembly.ToLower() == AssemblyName.ToLower())
                {
                    boolControlFound = true;
                }
            }
            return boolControlFound;
        }
        #endregion
 
        #region btnUpdateWebConfig_Click
        protected void btnUpdateWebConfig_Click(object sender, EventArgs e)
        {
            // Update system.codedom
            System.Configuration.Configuration rootWebConfig = 
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            DefaultSection DefaultSection = (DefaultSection)rootWebConfig.GetSection("system.codedom");
            SectionInformation SectionInformation = (SectionInformation)DefaultSection.SectionInformation;
 
            string strAppBase = Server.MapPath(this.TemplateSourceDirectory + @"\..\..\");
            string strFileNameAndPath = 
                String.Format("{0}DesktopModules\\LinqPrep\\ConfigurationKeys\\SystemCodeDom.txt", strAppBase);
            SectionInformation.SetRawXml(GetTextFile(strFileNameAndPath));
 
            // Update assemblies
            SystemWebSectionGroup SystemWebSectionGroup = 
                (SystemWebSectionGroup)rootWebConfig.GetSectionGroup("system.web");
            AssemblyCollection AssemblyCollection = SystemWebSectionGroup.Compilation.Assemblies;
 
            AddAssembly("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089", 
                AssemblyCollection);
            AddAssembly("System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089", 
                AssemblyCollection);
            AddAssembly("System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089", 
                AssemblyCollection);
            AddAssembly("System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", 
                AssemblyCollection);
            
            // Update Controls
            TagPrefixInfo SystemWebUI = 
                new TagPrefixInfo("asp","System.Web.UI","System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35","","");
            TagPrefixInfo SystemWebUIWebControls = 
                new TagPrefixInfo("asp", "System.Web.UI.WebControls", "System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35", "", "");
            SystemWebSectionGroup.Pages.Controls.Clear();
            SystemWebSectionGroup.Pages.Controls.Add(SystemWebUI);
            SystemWebSectionGroup.Pages.Controls.Add(SystemWebUIWebControls);
            
            rootWebConfig.Save();
            ShowStatus();
        }
        #endregion
 
        #region GetTextFile
        private String GetTextFile(string PathandScript)
        {
            string strTextFile;
            StreamReader reader = new StreamReader(PathandScript);
            strTextFile = reader.ReadToEnd();
            reader.Close();
            reader = null;
            return strTextFile;
        }
        #endregion
 
        #region AddAssembly
        void AddAssembly(string AssemblyName, AssemblyCollection ColAssemblyCollection)
        {
            if (!SearchForAssembly(AssemblyName, ColAssemblyCollection))
            {
                AssemblyInfo AssemblyInfo = new AssemblyInfo(AssemblyName);
                ColAssemblyCollection.Add(AssemblyInfo);
            }
        }
        #endregion
    }
}

 

[Back to: The ADefWebserver DotNetNuke HELP WebSite]


DotNetNuke® is a registered trademark of DotNetNuke Corporation