Question

My intentions is to be able to add and remove web.config changes when a solution is installed or uninstalled.

The code below and the powershell below works ALMOST as expected.

  1. When I add sp solution, and then install sp solution, The web.config is modified and a new entry is added. Perfect!!!!

However the problem is that when I execute the powershell scripts to uninstall and remove the sp solution, The entry is added again, instead of being removed.

WEIRD!!!!

 public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWebService contentService = SPWebService.ContentService;
                contentService.WebConfigModifications.Add(GetConfigModification());

                // Serialize the Web application state and propagate changes across the farm. 
                contentService.Update();

                // Save Web.config changes.
                contentService.ApplyWebConfigModifications();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                throw;
            }
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWebService contentService = SPWebService.ContentService;
                contentService.WebConfigModifications.Remove(GetConfigModification());

                // Serialize the Web application state and propagate changes across the farm. 
                contentService.Update();

                // Save Web.config changes.
                contentService.ApplyWebConfigModifications();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                throw;
            }
        }


        /// <summary>
        /// Get configuration to add or remove
        /// </summary>
        /// <returns></returns>
        public SPWebConfigModification GetConfigModification()
        {
            SPWebConfigModification modification = new SPWebConfigModification("authorizedType[@Assembly=\"MyCompany.CustomACtivities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=207090b9b3f674c8\"][@Namespace=\"MyCompany.CustomActivities\"][@TypeName=\"*\"][@Authorized=\"True\"]", "configuration/System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes");
            modification.Owner = "MyCompany.CustomActivities";
            modification.Sequence = 0;
            modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            modification.Value = string.Format(CultureInfo.InvariantCulture, "<authorizedType Assembly=\"{0}\" Namespace=\"{1}\" TypeName=\"{2}\" Authorized=\"{3}\"/>", new object[] { "MyCompany.CustomActivities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=207090b9b3f674c8", "MyCompany.CustomActivities", "*", "True" });
            Trace.TraceInformation("SPWebConfigModification value: {0}",modification.Value);
            return modification;        }

And the powershell

Add-SPSolution -LiteralPath "D:\MyCompany.CustomActivitiesActions\MyCompany.CustomConfig\bin\Debug\MyCompany.CustomConfig.wsp" 
Install-SPSolution -Identity MyCompany.CustomConfig.wsp -GACDeployment -Force

Uninstall-SPSolution –Identity MyCompany.CustomConfig.wsp 
Remove-SPSolution –Identity MyCompany.CustomConfig.wsp -force
Was it helpful?

Solution

The name of the SPConfigModification should be the exact xPath to find the element to remove within the container specified by path

In your code you have a Upper/Lowercase error in the name Assembly=\"MyCompany.CustomACtivities... !!! You should use constants (or better get the values from the type) for the values in both Name and Value to make sure they are the same

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top