Friday, May 12, 2017

Automatic web.config changes using a SharePoint, File Size Upload,Web Application level feature

By default for SharePoint, Machine.config is configured to accept HTTP Requests up to 4096 KB (4 MB) and it is reflected in all your ASP.NET applications. TO upload larger files, You can change the Machine.config file directly, or you can change only the Web.config file of the respective application(s). Ideal way is to change web.config of the relevant application.

Now if you have access to server, you can modify web.config directly. Open your Web.config file, and just below the tag, add the following tag:

<system.web>
<httpRuntime executionTimeout="999999" maxRequestLength="2097151" />.....

But if the same changes required to be made to UAT or PROD environment, most of the time developer does not have access to servers where they can access web.config. In that scenario we have to achieve this by developing WebApplication level feature, which does this changes for us.

Below is the class which does same job on feature activated.

public class customWebConfigEventReceiver : SPFeatureReceiver
    {
        string modOwner = "LargeFileUpload";

        // Uncomment the method below to handle the event raised after a feature has been activated.
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            RemoveAllCustomisations(webApp);

            Collection modsCollection = webApp.WebConfigModifications;
            bool configExist = false;
            for (int i = 0; i < modsCollection.Count; i++)
            {
                if (modsCollection[i].Owner == modOwner)
                {
                    configExist = true;
                    break;
                }
            }
            if (!configExist)
            {
                webApp.WebConfigModifications.Add(createWebConfigModification("/configuration/system.web/httpRuntime", "executionTimeout", modOwner, "999999"));
                webApp.WebConfigModifications.Add(createWebConfigModification("/configuration/system.web/httpRuntime", "maxRequestLength", modOwner, "2097151"));
                webApp.Update();
                webApp.Farm.Services.GetValue().ApplyWebConfigModifications();
            }


        }

        protected SPWebConfigModification createWebConfigModification(string sPath, string sName, string sOwner, string sValue)
        {
            //SPWebConfigModification myModification = new SPWebConfigModification(modName, modPath);
            SPWebConfigModification httpRuntimeModification = new SPWebConfigModification();
            httpRuntimeModification.Path = sPath;
            httpRuntimeModification.Name = sName;
            httpRuntimeModification.Sequence = 0;
            httpRuntimeModification.Owner = sOwner;
            httpRuntimeModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;
            httpRuntimeModification.Value = sValue;
            return httpRuntimeModification;
        }

        private void RemoveAllCustomisations(SPWebApplication webApp)
        {
            if (webApp != null)
            {
                Collection collection = webApp.WebConfigModifications;
                int iStartCount = collection.Count;

                // Remove any modifications that were originally created by the owner.
                for (int c = iStartCount - 1; c >= 0; c--)
                {
                    SPWebConfigModification configMod = collection[c];

                    if (configMod.Owner == modOwner)
                    {
                        collection.Remove(configMod);
                    }
                }

                // Apply changes only if any items were removed.
                if (iStartCount > collection.Count)
                {
                    webApp.Update();
                    webApp.Farm.Services.GetValue().ApplyWebConfigModifications();
                }
            }
        }

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

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;
            RemoveAllCustomisations(webApp);
            webApp.WebConfigModifications.Remove(createWebConfigModification("/configuration/system.web/httpRuntime", "executionTimeout", modOwner, "999999"));
            webApp.WebConfigModifications.Add(createWebConfigModification("/configuration/system.web/httpRuntime", "maxRequestLength", modOwner, "51200"));
            webApp.Update();
            webApp.Farm.Services.GetValue().ApplyWebConfigModifications();

        }
}

You can make settings value (True/False) in Feature Property based on need like,

  • Activate On Default
  • Always Force Install
  • Auto Active In Central Admin
Share:

0 comments:

Post a Comment