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.
webApp.WebConfigModifications.Add(modification);
// Save web.config changes.
webApp.Farm.Services.GetValue().ApplyWebConfigModifications();
// Serialize the web application state and propagate changes across the farm.
webApp.Update();


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 :
http://www.crsw.com/mark/Lists/Posts/Post.aspx?ID=32
http://blogs.devhorizon.com/reza/?p=459
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spwebconfigmodification.aspx



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″ ?>

<actions>

<add path=”configuration/appSettings”>

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

</add>

<add path=”configuration”>

<connectionStrings />

</add>

<add path=”configuration/connectionStrings”>

<remove name=”MySqlServerConnection” />

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

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

</add>

</actions>




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

Reference: http://claytonj.wordpress.com/2008/03/19/custom-webconfig-settings-in-sharepoint/


3. See also : http://blog.thekid.me.uk/archive/2007/03/24/web-config-modification-manager-for-sharepoint.aspx



No comments:

Post a Comment