1. Open Visual Studio 2010
2. Select New Project Empty SharePoint Project
3. Provide a local site and Select "Deploy as a farm solution" option.
(Mostly, developers
don't put anything in Description but this information really comes in handy
when you are trying to figure out what a specific Feature is supposed to do in
terms of functionality)
6.
Right Click on Feature1.Feature and
Click on "Add Event Receiver"7. Uncomment the "Feature Activated" part of the code
This code is added to the following entries in web.config
- Connection Strings
- 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();
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();
//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();
//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 this is to find all the entries that are inserted by Owner name "Smartrider".
public
override
void FeatureDeactivating(SPFeatureReceiverProperties properties)
override
void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = SPWebApplication.Lookup(new
Uri("URL"));
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)
//Remove Entries made to web.config by Owner name "Smartrider"
private
void RemoveEntries(SPWebApplication webApp)
{
try
try
{
List<SPWebConfigModification> entriesToRemove = new
List<SPWebConfigModification>();
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;
}
}
9. Deploy the code and check the
web.config file to verify your entries.10. Done
Hemant Datta is the COO for JHC Technology. He can be reached at hdatta(at)jhctechnology.com, @hdatta, or connect with him on
LinkedIn.