private String readAppSettings(GSBApplicationConfigManager gcm, String key)
        {
            String appSettingValue = gcm.GetNodeValue(String.Format("/configuration/appSettings/add[@key='{0}']", key));

            if (String.IsNullOrEmpty(appSettingValue))
            {
                return(String.Empty);
            }
            else
            {
                return(appSettingValue.Trim());
            }
        }
        public void LoadConfigurationFromFileToForm(string webConfigFilePath)
        {
            GSBApplicationConfigManager gcm = new GSBApplicationConfigManager();

            gcm.LoadXML(webConfigFilePath);
            string myVal = readAppSettings(gcm, "GSALocation");

            tbGSALocation.Text = myVal;

            myVal = readAppSettings(gcm, "siteCollection");
            tbSiteCollection.Text = myVal;

            myVal           = readAppSettings(gcm, "frontEnd");
            tbFrontEnd.Text = myVal;

            myVal = readAppSettings(gcm, "GSAStyle");
            if (myVal.ToLower().Equals("true"))
            {
                rbGSAFrontEnd.Checked = true;
            }
            else
            {
                rbCustomStylesheet.Checked = true;
            }

            myVal = readAppSettings(gcm, "verbose");
            if (myVal.ToLower().Equals("true"))
            {
                cbEnableLogging.Checked = true;
            }
            else
            {
                cbEnableLogging.Checked = false;
            }

            // Get value for accesslevel key from web.config and accordingly populate the radiobuttons
            myVal = readAppSettings(gcm, "accesslevel");
            if (myVal.ToLower().Equals("a"))
            {
                rbPublicAndSecure.Checked = true;
            }
            else if (myVal.ToLower().Equals("p"))
            {
                rbPublic.Checked = true;
            }

            // Get value for defaultSearchType key from web.config and accordingly populate the radiobuttons
            myVal = readAppSettings(gcm, "defaultSearchType");
            if (myVal.ToLower().Equals("publicandsecure"))
            {
                rbPublicAndSecureDefaultSearchType.Checked = true;
            }
            else if (myVal.ToLower().Equals("public"))
            {
                rbPublicDefaultSearchType.Checked = true;
            }

            enableFrontendConfiguration(rbGSAFrontEnd.Checked);
            enableDefaultSearchTypeConfiguration(rbPublicAndSecure.Checked);
            myVal = readAppSettings(gcm, "UseSamlPost");
            Boolean useSamlPost = myVal.ToLower().Equals("true") && !rbPublic.Checked;

            chkUseSAMLPost.Checked      = useSamlPost;
            chkUseSAMLPost.Enabled      = !rbPublic.Checked;
            txtCertName.Text            = readAppSettings(gcm, "certificate_friendly_name");
            txtArtifactConsumerURL.Text = readAppSettings(gcm, "assertion_consumer");
            txtIDPEntityID.Text         = readAppSettings(gcm, "idp_entity_id");
            txtTrustDuration.Text       = readAppSettings(gcm, "trust_duration");

            enableSamlPostConfiguration(useSamlPost);
            loadComplete = true;
        }
        public void SaveConfigurationsToFile(string webConfigFilePath,bool isInstaller)
        {
            GSBApplicationConfigManager gcm = new GSBApplicationConfigManager();

            gcm.LoadXML(webConfigFilePath);
            gcm.EnsureParentNodesForGSAParameters();//ensure that all the nodes are in place
                                                    //else create them

            gcm.ModifyNode("/configuration/appSettings", "siteCollection", SiteCollection);
            gcm.ModifyNode("/configuration/appSettings", "GSALocation", ApplianceLocation);
            gcm.ModifyNode("/configuration/appSettings", "frontEnd", FrontEnd);
            gcm.ModifyNode("/configuration/appSettings", "verbose", EnableLogging);
            gcm.ModifyNode("/configuration/appSettings", "GSAStyle", UseGsaStyling);//for custom stylesheet
            gcm.ModifyNode("/configuration/appSettings", "accesslevel", AccessLevel);//for 'public' or 'public and secure' search with GSBS
            gcm.ModifyNode("/configuration/appSettings", "omitSecureCookie", OmitSecureCookie);// Included for the BoA secure cookie issue. Will decide whether to process the cookie or discard the same.

            /*
            * Denotes the default search type used. Value "publicAndSecure" denotes the default search type will be 'public and secure',
            * and value "public" denotes the default search type will be 'public' search.
            */
            gcm.ModifyNode("/configuration/appSettings", "defaultSearchType", DefaultSearchType);

            // Denotes the name for the help file for search tips
            gcm.ModifyNode("/configuration/appSettings", "SearchTipsHTMLFileName", SearchTipsHTMLFileName);

            // Code for enabling Session State on SharePoint Web Application
            gcm.ModifyNodeForHttpModule("//httpModules", "Session", SessionStateModule);

            gcm.ModifyNode("/configuration/appSettings", "EnableEmbeddedMode", EnableEmbeddedMode.ToString().ToLower());
            // UseContainerTheme configuartion parameter value should be same as EnableEmbeddedMode
            gcm.ModifyNode("/configuration/appSettings", "UseContainerTheme", EnableEmbeddedMode.ToString().ToLower());

            gcm.ModifyNode("/configuration/appSettings", "UseSamlPost", UseSamlPost.ToString().ToLower());
            gcm.ModifyNode("/configuration/appSettings", "assertion_consumer", artifactConsumer);
            gcm.ModifyNode("/configuration/appSettings", "certificate_friendly_name", CertificateName);
            gcm.ModifyNode("/configuration/appSettings", "idp_entity_id", IDPEntityID);
            gcm.ModifyNode("/configuration/appSettings", "trust_duration", TrustDuration.ToString());

            //this needs to be saved only during installation. should be unchnaged otherwise
            if (isInstaller == true)
            {
                gcm.ModifyNode("/configuration/appSettings", "xslGSA2SP", GsaToSpStyle);//for custom stylesheet
                gcm.ModifyNode("/configuration/appSettings", "xslSP2result", SpToResultStyle);//for custom stylesheet

                //add for logging
                gcm.ModifyNode("/configuration/appSettings", "logLocation", logLocation);//for custom stylesheet
                // Setting filterParameter to "p" only during initial deployment.
                gcm.ModifyNode("/configuration/appSettings", "filterParameter", "p");
                // ShowScope and ShowSearchTips parameters to True only during initial deployment
                gcm.ModifyNode("/configuration/appSettings", "showScope", "true");
                gcm.ModifyNode("/configuration/appSettings", "showSearchTips", "true");
            }
            else
            {
                #region Scenario and Sample
                /*
                 Consider scenario: new web app created or user has clicked cancel on the "configuration wizard form" while putting the GSA parameters
                    -E.g. user has clicked cancel while saving the configuration
                    -so as a result the custom stylesheet #1 and #2 location is not getting saved.
                    Note: we can get the location while installation (as per folder location chosen by user).
                 */

                //Steps to ResolveEventArgs it:
                //1. Read the respective nodes form the web.config of as given web ApplicationException.
                //2. if ValueType = blank or null put value, get current dir and put the values accordingly

                //sample values
                //<add key="xslGSA2SP" value="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\GSA2SP.xsl" />
                //<add key="xslSP2result" value="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\SP_Actual.xsl" />
                #endregion Scenario and Sample

                string StylesheetPath= "";
                string CurrentDir = Directory.GetCurrentDirectory();//Get the current Directory value (common for all)

                //read style#1
                StylesheetPath = gcm.GetNodeValue("/configuration/appSettings/add[@key='xslGSA2SP']");
                if ((null == StylesheetPath) || (StylesheetPath.Trim().Equals("")))
                {
                    StylesheetPath = CurrentDir + "\\GSA2SP.xsl";//Get the value from web.config
                    gcm.ModifyNode("/configuration/appSettings", "xslGSA2SP", StylesheetPath);//modify node
                }

                //read style#2
                StylesheetPath = gcm.GetNodeValue("/configuration/appSettings/add[@key='xslSP2result']");
                if ((null == StylesheetPath) || (StylesheetPath.Trim().Equals("")))
                {
                    StylesheetPath = CurrentDir + "\\SP_Actual.xsl";//Get the value from web.config
                    gcm.ModifyNode("/configuration/appSettings", "xslSP2result", StylesheetPath);//modify node
                }
            }
            gcm.SaveXML();//finally save the resultant modified values
        }
        public void SaveConfigurationsToFile(string webConfigFilePath, bool isInstaller)
        {
            GSBApplicationConfigManager gcm = new GSBApplicationConfigManager();

            gcm.LoadXML(webConfigFilePath);
            gcm.EnsureParentNodesForGSAParameters(); //ensure that all the nodes are in place
                                                     //else create them

            gcm.ModifyNode("/configuration/appSettings", "siteCollection", SiteCollection);
            gcm.ModifyNode("/configuration/appSettings", "GSALocation", ApplianceLocation);
            gcm.ModifyNode("/configuration/appSettings", "frontEnd", FrontEnd);
            gcm.ModifyNode("/configuration/appSettings", "verbose", EnableLogging);
            gcm.ModifyNode("/configuration/appSettings", "GSAStyle", UseGsaStyling);            //for custom stylesheet
            gcm.ModifyNode("/configuration/appSettings", "accesslevel", AccessLevel);           //for 'public' or 'public and secure' search with GSBS
            gcm.ModifyNode("/configuration/appSettings", "omitSecureCookie", OmitSecureCookie); // Included for the BoA secure cookie issue. Will decide whether to process the cookie or discard the same.

            /*
             * Denotes the default search type used. Value "publicAndSecure" denotes the default search type will be 'public and secure',
             * and value "public" denotes the default search type will be 'public' search.
             */
            gcm.ModifyNode("/configuration/appSettings", "defaultSearchType", DefaultSearchType);

            // Denotes the name for the help file for search tips
            gcm.ModifyNode("/configuration/appSettings", "SearchTipsHTMLFileName", SearchTipsHTMLFileName);

            // Code for enabling Session State on SharePoint Web Application
            gcm.ModifyNodeForHttpModule("//httpModules", "//modules", "Session", SessionStateModule);

            gcm.ModifyNode("/configuration/appSettings", "EnableEmbeddedMode", EnableEmbeddedMode.ToString().ToLower());
            // UseContainerTheme configuartion parameter value should be same as EnableEmbeddedMode
            gcm.ModifyNode("/configuration/appSettings", "UseContainerTheme", EnableEmbeddedMode.ToString().ToLower());

            gcm.ModifyNode("/configuration/appSettings", "UseSamlPost", UseSamlPost.ToString().ToLower());
            gcm.ModifyNode("/configuration/appSettings", "assertion_consumer", artifactConsumer);
            gcm.ModifyNode("/configuration/appSettings", "certificate_friendly_name", CertificateName);
            gcm.ModifyNode("/configuration/appSettings", "idp_entity_id", IDPEntityID);
            gcm.ModifyNode("/configuration/appSettings", "trust_duration", TrustDuration.ToString());

            //this needs to be saved only during installation. should be unchnaged otherwise
            if (isInstaller == true)
            {
                gcm.ModifyNode("/configuration/appSettings", "xslGSA2SP", GsaToSpStyle);       //for custom stylesheet
                gcm.ModifyNode("/configuration/appSettings", "xslSP2result", SpToResultStyle); //for custom stylesheet

                //add for logging
                gcm.ModifyNode("/configuration/appSettings", "logLocation", logLocation);//for custom stylesheet
                // Setting filterParameter to "p" only during initial deployment.
                gcm.ModifyNode("/configuration/appSettings", "filterParameter", "p");
                // ShowScope and ShowSearchTips parameters to True only during initial deployment
                gcm.ModifyNode("/configuration/appSettings", "showScope", "true");
                gcm.ModifyNode("/configuration/appSettings", "showSearchTips", "true");
            }
            else
            {
                #region Scenario and Sample

                /*
                 * Consider scenario: new web app created or user has clicked cancel on the "configuration wizard form" while putting the GSA parameters
                 *  -E.g. user has clicked cancel while saving the configuration
                 *  -so as a result the custom stylesheet #1 and #2 location is not getting saved.
                 *  Note: we can get the location while installation (as per folder location chosen by user).
                 */

                //Steps to ResolveEventArgs it:
                //1. Read the respective nodes form the web.config of as given web ApplicationException.
                //2. if ValueType = blank or null put value, get current dir and put the values accordingly


                //sample values
                //<add key="xslGSA2SP" value="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\GSA2SP.xsl" />
                //<add key="xslSP2result" value="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\SP_Actual.xsl" />
                #endregion Scenario and Sample

                string StylesheetPath = "";
                string CurrentDir     = Directory.GetCurrentDirectory();//Get the current Directory value (common for all)

                //read style#1
                StylesheetPath = gcm.GetNodeValue("/configuration/appSettings/add[@key='xslGSA2SP']");
                if ((null == StylesheetPath) || (StylesheetPath.Trim().Equals("")))
                {
                    StylesheetPath = CurrentDir + "\\GSA2SP.xsl";                              //Get the value from web.config
                    gcm.ModifyNode("/configuration/appSettings", "xslGSA2SP", StylesheetPath); //modify node
                }

                //read style#2
                StylesheetPath = gcm.GetNodeValue("/configuration/appSettings/add[@key='xslSP2result']");
                if ((null == StylesheetPath) || (StylesheetPath.Trim().Equals("")))
                {
                    StylesheetPath = CurrentDir + "\\SP_Actual.xsl";                              //Get the value from web.config
                    gcm.ModifyNode("/configuration/appSettings", "xslSP2result", StylesheetPath); //modify node
                }
            }
            gcm.SaveXML();//finally save the resultant modified values
        }
        public void LoadConfigurationFromFileToForm(string webConfigFilePath)
        {
            GSBApplicationConfigManager gcm = new GSBApplicationConfigManager();
            gcm.LoadXML(webConfigFilePath);
            string myVal = readAppSettings(gcm, "GSALocation");
            tbGSALocation.Text = myVal;

            myVal = readAppSettings(gcm, "siteCollection");
            tbSiteCollection.Text = myVal;

            myVal = readAppSettings(gcm, "frontEnd");
            tbFrontEnd.Text = myVal;

            myVal = readAppSettings(gcm, "GSAStyle");
            if (myVal.ToLower().Equals("true"))
            {
                rbGSAFrontEnd.Checked = true;
            }
            else
            {
                rbCustomStylesheet.Checked = true;
            }

            myVal = readAppSettings(gcm, "verbose");
            if (myVal.ToLower().Equals("true"))
            {
                cbEnableLogging.Checked = true;
            }
            else
            {
                cbEnableLogging.Checked = false;
            }

            // Get value for accesslevel key from web.config and accordingly populate the radiobuttons
            myVal = readAppSettings(gcm, "accesslevel");
            if (myVal.ToLower().Equals("a"))
            {
                rbPublicAndSecure.Checked = true;
            }
            else if (myVal.ToLower().Equals("p"))
            {
                rbPublic.Checked = true;
            }

            // Get value for defaultSearchType key from web.config and accordingly populate the radiobuttons
            myVal = readAppSettings(gcm, "defaultSearchType");
            if (myVal.ToLower().Equals("publicandsecure"))
            {
                rbPublicAndSecureDefaultSearchType.Checked = true;
            }
            else if (myVal.ToLower().Equals("public"))
            {
                rbPublicDefaultSearchType.Checked = true;
            }

            enableFrontendConfiguration(rbGSAFrontEnd.Checked);
            enableDefaultSearchTypeConfiguration(rbPublicAndSecure.Checked);
            myVal = readAppSettings(gcm, "UseSamlPost");
            Boolean useSamlPost = myVal.ToLower().Equals("true") && !rbPublic.Checked;
            chkUseSAMLPost.Checked = useSamlPost;
            chkUseSAMLPost.Enabled = !rbPublic.Checked;
            txtCertName.Text = readAppSettings(gcm, "certificate_friendly_name");
            txtArtifactConsumerURL.Text = readAppSettings(gcm, "assertion_consumer");
            txtIDPEntityID.Text = readAppSettings(gcm, "idp_entity_id");
            txtTrustDuration.Text = readAppSettings(gcm, "trust_duration");

            enableSamlPostConfiguration(useSamlPost);
            loadComplete = true;
        }
 private String readAppSettings(GSBApplicationConfigManager gcm, String key)
 {
     String appSettingValue = gcm.GetNodeValue(String.Format("/configuration/appSettings/add[@key='{0}']", key));
     if (String.IsNullOrEmpty(appSettingValue))
     {
         return String.Empty;
     }
     else
     {
         return appSettingValue.Trim();
     }
 }
        static void Main(string[] args)
        {
            #region CleanupGsaWebApplicationSettings
            try
            {
                foreach (SPWebApplication wa in SPWebService.AdministrationService.WebApplications)
                {
                    try
                    {
                        if (GSBControlPanel.frmWebApplicationList.isLocalWebApplication(wa))
                        {
                            //Check if the web application are from the same machine
                            string path = wa.IisSettings[0].Path.ToString();
                            if (null != path)
                            {
                                if (!path.EndsWith("\\"))
                                {
                                    path += "\\" + "web.config";
                                }
                                else
                                {
                                    path += "web.config";
                                }

                                #region delete APP setting nodes
                                GSBApplicationConfigManager mgr = new GSBApplicationConfigManager();
                                mgr.LoadXML(path);
                                mgr.DeleteNode("/configuration/appSettings/add[@key='siteCollection']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='GSALocation']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='frontEnd']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='verbose']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='accesslevel']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='omitSecureCookie']");

                                //for custom stylesheet
                                mgr.DeleteNode("/configuration/appSettings/add[@key='xslGSA2SP']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='xslSP2result']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='GSAStyle']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='logLocation']");

                                // Deleting the HTTPModule corresponding to the session state from web.config file
                                mgr.DeleteNode("//httpModules//add[@name='Session']");

                                // Deleting the node corresponding to default search type
                                mgr.DeleteNode("/configuration/appSettings/add[@key='defaultSearchType']");

                                // Deleting the node corresponding to the HTML help file for Search Tips
                                mgr.DeleteNode("/configuration/appSettings/add[@key='SearchTipsHTMLFileName']");

                                 // Embedded mode related app settings
                                mgr.DeleteNode("/configuration/appSettings/add[@key='EnableEmbeddedMode']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='UseContainerTheme']");

                                // filterParameter app settings
                                mgr.DeleteNode("/configuration/appSettings/add[@key='filterParameter']");

                                // Deleting the node corresponding to the SAML POST
                                mgr.DeleteNode("/configuration/appSettings/add[@key='UseSamlPost']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='assertion_consumer']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='certificate_friendly_name']");

                                mgr.SaveXML();
                                #endregion save results to file
                            }
                        }//if (isLocalWebApplication(wa))
                    }
                    catch { }
                }
            }
            catch { }

            try
            {
                foreach (SPWebApplication wa in SPWebService.ContentService.WebApplications)
                {
                    try
                    {
                        //need to check if the web application are from the same machine
                        if (GSBControlPanel.frmWebApplicationList.isLocalWebApplication(wa))
                        {
                            string path = wa.IisSettings[0].Path.ToString();
                            if (null != path)
                            {
                                if (!path.EndsWith("\\"))
                                {
                                    path += "\\" + "web.config";
                                }
                                else
                                {
                                    path += "web.config";
                                }
                                #region delete APP setting nodes
                                GSBApplicationConfigManager mgr = new GSBApplicationConfigManager();
                                mgr.LoadXML(path);
                                mgr.DeleteNode("/configuration/appSettings/add[@key='siteCollection']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='GSALocation']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='frontEnd']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='verbose']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='accesslevel']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='omitSecureCookie']");

                                //for custom stylesheet
                                mgr.DeleteNode("/configuration/appSettings/add[@key='xslGSA2SP']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='xslSP2result']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='GSAStyle']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='logLocation']");

                                // Deleting the HTTPModule corresponding to the session state from web.config file
                                mgr.DeleteNode("//httpModules//add[@name='Session']");

                                // Deleting the node corresponding to default search type
                                mgr.DeleteNode("/configuration/appSettings/add[@key='defaultSearchType']");

                                // Deleting the node corresponding to the HTML help file for Search Tips
                                mgr.DeleteNode("/configuration/appSettings/add[@key='SearchTipsHTMLFileName']");

                                // Embedded mode related app settings
                                mgr.DeleteNode("/configuration/appSettings/add[@key='EnableEmbeddedMode']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='UseContainerTheme']");

                                // filterParameter app settings
                                mgr.DeleteNode("/configuration/appSettings/add[@key='filterParameter']");

                                // Deleting the node corresponding to the SAML POST
                                mgr.DeleteNode("/configuration/appSettings/add[@key='UseSamlPost']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='assertion_consumer']");
                                mgr.DeleteNode("/configuration/appSettings/add[@key='certificate_friendly_name']");

                                mgr.SaveXML();
                                #endregion delete APP setting nodes
                            }
                        }
                    }
                    catch { }
                }
            }
            catch { }
            #endregion CleanupGsaWebApplicationSettings

            try
            {
                String myBasePath = args[0];//12 hive
                //Note: due to a bug in installshield, we get additional Quote(") in the path. It needs to be handled
                if (myBasePath.EndsWith("\""))
                {
                    myBasePath = myBasePath.Substring(0, myBasePath.Length - 1);
                }

                if (!myBasePath.EndsWith("\\"))
                {
                    myBasePath += "\\";
                }
                try
                {
                    UnInstallFeature(myBasePath);//Deactivate and Un-Install GSA search feature
                }
                catch (Exception)
                { }
            }
            catch (Exception)
            {
                //do nothing ... as can't log exception while installation
            }
        }