As a developer, Many time requirements come to change the web cofig file of sharepoint.
We can achve this by writing some C# codes.
Modifying Web Config File:
Removing web config modifications made by user
We can achve this by writing some C# codes.
Modifying Web Config File:
Lets assume, we are integrating third party DLL in our application, So we need to place that dll file in bin folder. along with that we also need to modify the trust level attribute from WSS_Minimal to Full
in the web config file of sharepoint.
I recommend to write in feature activation.
|
//Gets the current web application from feature properties.
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication; // Declairs the webConfigmodification variable. SPWebConfigModification myModification = newSPWebConfigModification("level", "configuration/system.web/trust"); //Gets a collection of web config modification. System.Collections.ObjectModel.Collection<SPWebConfigModification> allModifications = webApp.WebConfigModifications;
myModification.Value = "Full";myModification.Owner = "OwnerName";
myModification.Sequence = 1; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute; allModifications.Add(myModification); // Add the modifications. SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); webApp.Update(); // Update the Web application. |
Removing web config modifications made by user
We also need to remove all the
modifications while deactivating the feature.
I recommend to write in feature deactivating event of the same feature which made those modifications.
I recommend to write in feature deactivating event of the same feature which made those modifications.
// Get the current web application from featutre property.
SPWebApplication webApp = properties.Feature.Parent asSPWebApplication;
// Declairs the webConfigmodification variable.
Collection<SPWebConfigModification> modificationCollection =
webApp.WebConfigModifications;
// Declairs the webConfigmodification variable.
Collection<SPWebConfigModification> removeCollection =
newCollection<SPWebConfigModification>();
int count = modificationCollection.Count;
for (int i = 0; i < count; i++)
{
SPWebConfigModification modification = modificationCollection[i];
// check and get all the recent modifications made by "TwitterWebPart" owner.
if (modification.Owner == "OwnerName")
{
removeCollection.Add(modification); // collect modifications to delete
}
}
// now delete the modifications from the web application
if (removeCollection.Count > 0)
{
foreach (SPWebConfigModification modificationItem in removeCollection)
{ .
//remove the modifications from web config
webApp.WebConfigModifications.Remove(modificationItem);
}
}
SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
webApp.Update();
SPWebApplication webApp = properties.Feature.Parent asSPWebApplication;
// Declairs the webConfigmodification variable.
Collection<SPWebConfigModification> modificationCollection =
webApp.WebConfigModifications;
// Declairs the webConfigmodification variable.
Collection<SPWebConfigModification> removeCollection =
newCollection<SPWebConfigModification>();
int count = modificationCollection.Count;
for (int i = 0; i < count; i++)
{
SPWebConfigModification modification = modificationCollection[i];
// check and get all the recent modifications made by "TwitterWebPart" owner.
if (modification.Owner == "OwnerName")
{
removeCollection.Add(modification); // collect modifications to delete
}
}
// now delete the modifications from the web application
if (removeCollection.Count > 0)
{
foreach (SPWebConfigModification modificationItem in removeCollection)
{ .
//remove the modifications from web config
webApp.WebConfigModifications.Remove(modificationItem);
}
}
SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
webApp.Update();
No comments:
Post a Comment