Wednesday 23 April 2014

Read sharepoint web.config using object model...


We need to initialize the necessary variables for ex as below

           //Initialize the variables
            string configFilePath = string.Empty;
            XmlDocument webConfigDoc = null;
            string appSettingVal = string.Empty;
            string xpath = string.Empty;

           configFilePath =  rootWeb.Site.WebApplication.GetIisSettingsWithFallback(Microsoft.SharePoint.Administration.SPUrlZone.Default).Path.FullName.ToString() + "\\web.config";
           webConfigDoc = new XmlDocument();
           webConfigDoc.Load(configFilePath);

           //Reading web.config attributes
           xpath = "configuration/appSettings/add[@key='" + <key value that you are looking for> + "']";
           XmlNode appSetting = webConfigDoc.SelectSingleNode(xpath);

            if (appSetting != null)
                    {
                       //gets the value for the key from appsettings section.
                        appSettingVal = appSetting.Attributes["value"].Value;
                    }

No comments:

Post a Comment