Archive

Posts Tagged ‘CodePlex’

How to add and remove entries from Web.Config using SPWebConfigModification

March 3, 2011 3 comments

Often times during SharePoint Development , we need to store configuration related entries in the web.config file. We can use Property Bag tool from CodePlex but in case you would like to use a Feature

then here are the steps to create a Feature that will add entries to the web.config for the SharePoint Web Application.

  • Open Visual Studio 2010
  • Select New à Project à Empty SharePoint Project
  • Provide a local site and Select “Deploy as a farm solution” option.

     

  • Right Click on the “Features” and Select “Add Feature”

     

  • On the following screen, provide the Unique Title Name and Description. (Refer to screen below) .

    (Mostly developers don’t put anything in Description but this information really comes in handy when you are trying to figure out what is a specific Feature suppose to do in terms of functionality)

  • Right Click on Feature1.Feature and Click on “Add Event Receiver”
  •  

     

 

  • Uncomment the “Feature Activated” part of the code

 

 

 

 

 

  • Add the following Code in FeatureActivated Method

This code is adding the following entries in web.config

  1. Connection Strings
  2. Key Value pair in AppSettings section

For each child node entry provide a specific owner name (like “Smartrider” in my case).We will need this as a reference later on in case you want to remove the entries (we are going to do this during Deactivation)

 

string value = @”Test”


SPWebApplication webApp = SPWebApplication.Lookup(new
Uri(“<URL>”));


SPWebService service = SPWebService.ContentService;


SPWebConfigModification connMod = new
SPWebConfigModification();

connMod.Path = “configuration”;

connMod.Name = “connectionStrings”;

connMod.Value = ” <connectionStrings> </connectionStrings>”;

connMod.Owner = Assembly.GetExecutingAssembly().FullName;

connMod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureSection;

webApp.WebConfigModifications.Clear();

webApp.WebConfigModifications.Add(connMod);


//SQL Connetion String


SPWebConfigModification webConfigMod = new
SPWebConfigModification();

webConfigMod.Owner = “Smartrider”;

webConfigMod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;

webConfigMod.Value = String.Format(“<add name=\”{0}\” connectionString=\”{1}\” providerName=\”{2}\”></add>”, “conn”, “Data Source=<datasource>;Initial Catalog=<databasename>;Integrated Security=True”, “System.Data.SQLClient”);

webConfigMod.Path = “configuration/connectionStrings”;

webConfigMod.Name = “sqlConn”;

webApp.WebConfigModifications.Add(webConfigMod);


//Add NetworkShare URL


SPWebConfigModification appConfigMod = new
SPWebConfigModification();

appConfigMod.Owner = “Smartrider”

appConfigMod.Path = “configuration/appSettings”;

appConfigMod.Name = String.Format(“add [@key=’NetworkURL’] [@value='{0}’]”, value);

appConfigMod.Value = String.Format(“<add key=’NetworkURL’ value='{0}’ />”, value);

appConfigMod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;

webApp.WebConfigModifications.Add(appConfigMod);

webApp.Update();

service.ApplyWebConfigModifications();

  • You also want to add code to remove the entry when the Feature is Deactivated. One way to do find all the entries that are inserted by Owner name “Smartrider”.

     


public
override
void FeatureDeactivating(SPFeatureReceiverProperties properties)

{


SPWebApplication webApp = SPWebApplication.Lookup(new
Uri(“URL”));


try

{

RemoveEntries(webApp);

webApp.Update();

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

webApp.WebConfigModifications.Clear();

}


catch (Exception ex)

{


throw ex;

}

}


//Remove Entries made to web.config by Owner name “Smartrider”


private
void RemoveEntries(SPWebApplication webApp)

{


try

{


List<SPWebConfigModification> entriesToRemove = new
List<SPWebConfigModification>();


foreach(SPWebConfigModification configMod in webApp.WebConfigModifications)

{


if(configMod.Owner == “Smartrider”)

{

entriesToRemove.Add(configMod);

}

}


if(entriesToRemove.Count > 0)

{


for (int i = entriesToRemove.Count – 1; i >= 0; i–)

{

webApp.WebConfigModifications.Remove(entriesToRemove[i]);

}

}

}


catch

{


throw;

}

}

  • Deploy the code and check the web.config file to verify your entries.
  • Done