//add all web.config entries
private void InsertWebConfigEntries(SPFeatureReceiverProperties properties)
{
if (!CheckEntries(properties))
{
using (SPSite site = properties.Feature.Parent as SPSite)
{
SPWebConfigModification dsEntry = GenerateWebConfigEntry("New config key", "configuration/system.web/compilation/assemblies",
"<add assembly=\"Bartomolina.SharePoint.SPBM.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789abcdef\" />", 100);
SPWebApplication currentWebApp = site.WebApplication;
currentWebApp.WebConfigModifications.Add(dsEntry);
currentWebApp.WebService.ApplyWebConfigModifications();
currentWebApp.Update();
}
}
}
//check if the web.config entry exists. If you don't call this method you will get duplicate entries
private bool CheckEntries(SPFeatureReceiverProperties properties)
{
using (SPSite site = properties.Feature.Parent as SPSite)
{
SPWebApplication currentWebApp = site.WebApplication;
Collection<SPWebConfigModification> webConfigEntries = currentWebApp.WebConfigModifications;
foreach (SPWebConfigModification entry in webConfigEntries)
{
if (entry.Owner == "ReceiverClass")
{
return true;
}
}
return false;
}
}
//new web.config entry
private SPWebConfigModification GenerateWebConfigEntry(string modName, string sXpath, string sKey, uint iSequence)
{
SPWebConfigModification configMod = new SPWebConfigModification(modName, sXpath);
configMod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
configMod.Name = string.Format("add[@name='{0}']", modName);
configMod.Owner = "ReceiverClass";
configMod.Value = sKey;
configMod.Sequence = iSequence;
return configMod;
}
No comments:
Post a Comment