Wednesday, February 25, 2009

Update Moss Web.Config

1. Programatically using Sharepoint API

We can use SPWebConfigModification object like this:

This code update CustomErrors value

// Get an instance of my local web application
SPWebApplication webApp = new SPSite("http://localhost").WebApplication;
// Create my new modification to set the mode attibute to "Off".
// Example:
SPWebConfigModification modification = new SPWebConfigModification("mode", "system.web/customErrors");
modification.Owner = "SimpleSampleUniqueOwnerValue";

modification.Sequence = 0; modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;
modification.Value = "Off";
// Add my new web.config modification.
// Save web.config changes.
// Serialize the web application state and propagate changes across the farm.

This code Add safe control entry to the web.config :

SPWebService myService = SPWebService.ContentService;
SPWebConfigModification myModification = new SPWebConfigModification();
myModification.Path = "configuration/SharePoint/SafeControls";
myModification.Name = "SafeControl[@Assembly='MyCustomAssembly'][@Namespace='MyCustomNamespace'][@TypeName='*'][@Safe='True']";
myModification.Sequence = 0;
myModification.Owner = WebConfigModificationFeatureReceiver.OwnerId;
myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
myModification.Value = "";
myService.WebConfigModifications.Add(myModification);myService.Update(); myService.ApplyWebConfigModifications();

References :

2. Deploying custom XML File in the Sharepoint CONFIG folder ( C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG

Steps to perform it:

1. Write an xml file called webconfig.[myname].xml

2. Add yours web.config sections into "actions" node:

<?xml version=”1.0″ encoding=”utf-8″ ?>


<add path=”configuration/appSettings”>

<add key=”MyFilePath” value=”C:\temp\path\” />


<add path=”configuration”>

<connectionStrings />


<add path=”configuration/connectionStrings”>

<remove name=”MySqlServerConnection” />

<add name=”MySqlServerConnection” connectionString=”server=[server];database=

db];Integrated Security=SSIP;” providerName=”System.Data.SqlClient” />



3. Copy your xml file into C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG folder

When you'll create a new web application or extend web application the web.config will be updated with yours sections by merging them into the web.config.

If you want to immediately apply the changes you have to run : stsadm –o copyappbincontent


3. See also :

No comments:

Post a Comment