Ybbest
Ybbest

Reputation: 1550

What is the best way for modifying web.config in SharePoint using code

What is the best way for modifying web.config in SharePoint using code? I could use SPWebConfigModification or powershell.

Upvotes: 0

Views: 1327

Answers (2)

Vighnesh Bendre
Vighnesh Bendre

Reputation: 26

You can do web.config modifications from VS 2010 code. See below code for sample of the same. I have implemented

            //Add tagMapping
            string modificationName = string.Format(@"add[@tagType='Microsoft.SharePoint.WebControls.PeopleEditor'][@mappedTagType='PeopleEditor.CustPeopleFind,{0}']", 
                Assembly.GetExecutingAssembly().FullName);
            SPWebConfigModification modification = new SPWebConfigModification //(modificationName, "configuration/system.web/pages/tagMapping");
            {
                Path = "configuration/system.web/pages/tagMapping",
                Name = modificationName,
                Value = string.Format(@"<add tagType=""Microsoft.SharePoint.WebControls.PeopleEditor"" mappedTagType=""PeopleEditor.CustPeopleFind"" />"),
                Owner = ownerID,
                Sequence = 1,
                Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode                    
            };
            webApp.WebConfigModifications.Add(modification);

            //Save changes
            webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

            //Serialize and propagate changes across farm
            webApp.Update();

            //Create the persistent objects
            Common.CreatePersistentObjects(webApp);

But, I should also warne you, many SharePoint experts do not recommend modifications done to web.config through code.

All the best

-Vighnesh

Upvotes: 1

Ashish Patel
Ashish Patel

Reputation: 1306

If you want to do it using code, SPWebConfigModification will be a good choice because that will take care of web.config modifications across all front end web servers in your farm. Doing it via PowerShell is almost like modifying it manually which is discouraged practice for SharePoint enviornment.

If you are using FEATURES, you may want to put your code in Feature Receivers.

If declarative modifications serves your need, you may prefer that (over code approach) as well. Entries like SafeControl are natively supported by Feature framework which should be the first choice for such entries. Another variation of declarative modifications is via supplemental .config file as described below:

http://msdn.microsoft.com/en-us/library/ms460914.aspx

Upvotes: 2

Related Questions