locked
SP Web Application Web.Config RRS feed

  • Question

  • Hi All,

    I want to use two web.config files in SharePoint Web application, the scenario is that in I am writing some custom functionalities, testing some new things in share point(eg using Enterprise Library Blocks) etc etc. So in order to make these things work i need to insert some code(Custom Configurations Like adding new Sections etc etc) in the Web.config file(Located at the respective virtual Directory). I want to keep my default web.config as it is , and want to do all my custom modification in another config file.

    Hope this makes sense.

     

    Regards:

    Aadil


    Thursday, December 17, 2009 5:46 AM

Answers

  • In fact, you can only keep one web.config file. The other one(backup one) won't  be functional since ASP.NET only allows one web.config in the directory.

    You can reference SPWebConfigModification Class (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spwebconfigmodification.aspx ) to modify your web.config.
     
    You can reference the sample code that is warped in a feature receiver:
     
    using System;
    using System.Collections;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
     
    namespace MS.Samples.SharePoint.WebConfigModification
    {
        public class WebConfigModificationFeatureReceiver : SPFeatureReceiver
        {
            private static String OwnerId = "WebConfigModificationSample" ;
     
            public override void   FeatureActivated(SPFeatureReceiverProperties properties)
            {
                   ;
            }
     
            public override void   FeatureDeactivating(SPFeatureReceiverProperties properties)
            {
                   ;
            }
     
            public override void FeatureInstalled(SPFeatureReceiverProperties properties)
            {
                SPWebService service = SPWebService.ContentService;
     
                this .RemoveExistingWebConfigModifications();
     
                SPWebConfigModification mod = new SPWebConfigModification();
                mod.Path = "configuration/SharePoint/SafeControls" ;
                mod.Name = "SafeControl[@Assembly='MYCUSTOMASSEMBLY'][@Namespace='MYCUSTOMNAMESPACE'][@TypeName='*'][@Safe='True']" ;
                mod.Sequence = 0;
                mod.Owner = WebConfigModificationFeatureReceiver.OwnerId;
                mod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                mod.Value = "<SafeControl Assembly='MYCUSTOMASSEMBLY' Namespace='MYCUSTOMNAMESPACE' TypeName='*' Safe='True' />" ;
     
                service.WebConfigModifications.Add(mod);
     
                service.Update();
     
                service.ApplyWebConfigModifications();
     
            }
     
            private IList GetExistingWebConfigModifications()
            {
                SPWebService service = SPWebService.ContentService;
                ArrayList myWebConfigMods = new ArrayList ();
     
                foreach (SPWebConfigModification mod in service.WebConfigModifications)
                {
                    if (mod.Owner == WebConfigModificationFeatureReceiver.OwnerId)
                    {
                        myWebConfigMods.Add(mod);
                    }
                }
     
                return myWebConfigMods;
            }
     
            private void RemoveExistingWebConfigModifications()
            {
                SPWebService service = SPWebService.ContentService;
               
                IList itemsToRemove = this .GetExistingWebConfigModifications();
     
                foreach (SPWebConfigModification mod in itemsToRemove)
                {
                    service.WebConfigModifications.Remove(mod);
                }
            }
     
            public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
            {
                SPWebService service = SPWebService.ContentService;
               
                this .RemoveExistingWebConfigModifications();
     
                service.ApplyWebConfigModifications();
            }
        }
    }


    Cogito ergo sum.
    • Proposed as answer by Farhan_ Ansari Friday, December 18, 2009 5:50 AM
    • Marked as answer by Chengyi Wu Thursday, December 24, 2009 4:42 AM
    Friday, December 18, 2009 1:17 AM

All replies

  • Hi,

    If you are using Enterprise library (which provide UI for adding section to web.config) then you don't need to use different web.config file. Since web.config file is an xml file and you can use add different sections to the web config file as long as the schema supports. If you have very custom configuration then you can use different config file (say config.xml) and put the file in the virtual directory. And then with some static class you can load the configuration to an object at runtime and then you can use the configuration over time. But using this custom config file you will not get built in support that you would get with web.config file. For example you can't use WebConfigurationManager class to access your custom config file. Also if you modify custom configuration file then the web site will be restarted automatically to reflect changes that you wold get useful if you use web.conf file.


    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com
    Thursday, December 17, 2009 9:13 AM
  • In fact, you can only keep one web.config file. The other one(backup one) won't  be functional since ASP.NET only allows one web.config in the directory.

    You can reference SPWebConfigModification Class (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spwebconfigmodification.aspx ) to modify your web.config.
     
    You can reference the sample code that is warped in a feature receiver:
     
    using System;
    using System.Collections;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
     
    namespace MS.Samples.SharePoint.WebConfigModification
    {
        public class WebConfigModificationFeatureReceiver : SPFeatureReceiver
        {
            private static String OwnerId = "WebConfigModificationSample" ;
     
            public override void   FeatureActivated(SPFeatureReceiverProperties properties)
            {
                   ;
            }
     
            public override void   FeatureDeactivating(SPFeatureReceiverProperties properties)
            {
                   ;
            }
     
            public override void FeatureInstalled(SPFeatureReceiverProperties properties)
            {
                SPWebService service = SPWebService.ContentService;
     
                this .RemoveExistingWebConfigModifications();
     
                SPWebConfigModification mod = new SPWebConfigModification();
                mod.Path = "configuration/SharePoint/SafeControls" ;
                mod.Name = "SafeControl[@Assembly='MYCUSTOMASSEMBLY'][@Namespace='MYCUSTOMNAMESPACE'][@TypeName='*'][@Safe='True']" ;
                mod.Sequence = 0;
                mod.Owner = WebConfigModificationFeatureReceiver.OwnerId;
                mod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                mod.Value = "<SafeControl Assembly='MYCUSTOMASSEMBLY' Namespace='MYCUSTOMNAMESPACE' TypeName='*' Safe='True' />" ;
     
                service.WebConfigModifications.Add(mod);
     
                service.Update();
     
                service.ApplyWebConfigModifications();
     
            }
     
            private IList GetExistingWebConfigModifications()
            {
                SPWebService service = SPWebService.ContentService;
                ArrayList myWebConfigMods = new ArrayList ();
     
                foreach (SPWebConfigModification mod in service.WebConfigModifications)
                {
                    if (mod.Owner == WebConfigModificationFeatureReceiver.OwnerId)
                    {
                        myWebConfigMods.Add(mod);
                    }
                }
     
                return myWebConfigMods;
            }
     
            private void RemoveExistingWebConfigModifications()
            {
                SPWebService service = SPWebService.ContentService;
               
                IList itemsToRemove = this .GetExistingWebConfigModifications();
     
                foreach (SPWebConfigModification mod in itemsToRemove)
                {
                    service.WebConfigModifications.Remove(mod);
                }
            }
     
            public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
            {
                SPWebService service = SPWebService.ContentService;
               
                this .RemoveExistingWebConfigModifications();
     
                service.ApplyWebConfigModifications();
            }
        }
    }


    Cogito ergo sum.
    • Proposed as answer by Farhan_ Ansari Friday, December 18, 2009 5:50 AM
    • Marked as answer by Chengyi Wu Thursday, December 24, 2009 4:42 AM
    Friday, December 18, 2009 1:17 AM