Question

I need to add a new line in web.config of a programmatically web application in SharePoint 2016. So I created a feature with a scope WebApplication like below:

 public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWebApplication application = properties.Feature.Parent as SPWebApplication;

        if (application != null)

        {
            Collection<SPWebConfigModification> modifications = new Collection<SPWebConfigModification>();
            SPWebConfigModification myModification;
            myModification = new SPWebConfigModification();
            myModification.Path = "configuration/SharePoint/SafeMode/PageParserPaths";
            myModification.Name = "PageParserPath[@VirtualPath='/_catalogs/masterpage/*'][@CompilationMode='Always'][@AllowServerSideScript='true'][@AllowUnsafeControls='True'][@IncludeSubFolders='True']";
            myModification.Sequence = 0;
            myModification.Owner = properties.Feature.Definition.Id.ToString();
            myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            myModification.Value = "<PageParserPath VirtualPath='/_catalogs/masterpage/*' CompilationMode='Always' AllowServerSideScript='true' AllowUnsafeControls='true' IncludeSubFolders='True'/>";


            modifications.Add(myModification);
            AddWebConfigModifications(application, modifications);
        }
    }
    public static void AddWebConfigModifications(SPWebApplication webApp, IEnumerable<SPWebConfigModification> modifications)
    {
        // THE ORIGINAL VALUE OF REMOTE ADMINISTRATOR
        var remoteAdministratorAccessDenied = SPWebService.ContentService.RemoteAdministratorAccessDenied;
        try
        {

            SPSecurity.RunWithElevatedPrivileges(delegate ()
            {
                // SET THE REMOTE ADMINISTATOR ACCESS DENIED FALSE
                SPWebService.ContentService.RemoteAdministratorAccessDenied = false;

                webApp.WebConfigModifications.Clear();
                foreach (SPWebConfigModification modification in modifications)
                {
                    SPWebService.ContentService.WebApplications[webApp.Id].WebConfigModifications.Add(modification);
                    
                    
                }
                SPWebService.ContentService.WebApplications[webApp.Id].Update();
                SPWebService.ContentService.WebApplications[webApp.Id].WebService.ApplyWebConfigModifications();

            });
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            // SET THE REMOTE ADMINISTATOR ACCESS DENIED BACK WHAT // IT WAS
            SPWebService.ContentService.RemoteAdministratorAccessDenied = remoteAdministratorAccessDenied;
        }

    }

My feature is: scopre webapplication, activated with powershell and not with visual studio.

My problem is the feature affect all the webapplication in my farm, all the web.config of all webapplication got this update ?

Any idea to update only one WebApplication.

No correct solution

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