CJ

The circle of life – ** .Net – Training – MOSS **

  • Past Post

  • Vistor Locations

Custom web.config settings in SharePoint

Posted by Clayton James on March 19, 2008

I have come across two ways to modify the web.config with custom nodes when using SharePoint. Yes, I know SharePoint solutions (.wsp) allow you to update web.config for safe control entries and other areas but this model doesn’t allow for any modification you want…lets say a WCF service <system.serviceModel>.

Option 1
You can use the SPWebConfigModification class that is inside the Microsoft.SharePoint.Administration.dll. Its purpose is to write nodes and attributes into the web.config file. This is a great approach when you want to deploy your custom settings via a features/solutions deployment.

SPWebService service = 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 = “User Name“; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Value = “<SafeControl Assembly=’MyCustomAssembly’ Namespace=’MyCustomNamespace’ TypeName=’*’ Safe=’True’ />”; service.WebConfigModifications.Add(myModification); /*Call Update and ApplyWebConfigModifications to save changes*/ service.Update(); service.ApplyWebConfigModifications();

 http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.administration.spwebconfigmodification.aspx

So the above code would go in a FeatureReceiver event when activated and removed when deactivated.

Option 2
If you want to write custom nodes into a web.config when the web application is first created you could also do the following.

When a Web Application is first created WSS copies the web.config file from C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG to the root folder of the Web Application. But before this file is copied it checks the CONFIG directory for any xml file that has a name in the format webconfig.*.xml and merges the contents with the web.config.

So you would create a file called webconfig.myname.xml and save it to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG

However, this approach will not modify existing web applications.

webconfig.myname.xml contents:
<?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>

So this needs to be completed on each web front end server. So to eliminate the manual approach again you can and package this up into a solution so WSS manages the deployment across the farm.
Add the file to a solution. A file to be deployed to the CONFIG folder can be inserted by using the <RootFile> element:

manifest.xml example:
<Solution xmlns=”http://schemas.microsoft.com/sharepoint/&#8221; SolutionId=”GUIDHERE”>
<RootFiles>
   <RootFile Location=”CONFIG\webconfig.myname.xml”/>
</RootFiles>
<!– rest of solution manifest here
<FeatureManifests>… and other elements
–>
</Solution>

http://msdn2.microsoft.com/en-gb/library/ms439965.aspx

About these ads

17 Responses to “Custom web.config settings in SharePoint”

  1. rich said

    You might want to read this blog for a differing opinion on Option 1. You can debate it’s validity.

    http://www.cleverworkarounds.com/2008/04/04/guru-of-governance/

  2. Randy Bouquet said

    CJ,

    Thank you for the ideas. I used option two in a SharePoint application and had it deployed via the WSP. The file was successfully placed into the 12/CONFIG folder, however, the changes did not appear in the web.config file. It was then I saw the “However, this approach will not modify existing web applications” sentence. I am hoping you might know of a way to do what I am trying to acheive. I would like to deploy changes to the web.config as part of the WSP so I can have changes appropriately pushed to all of the servers on the farm. Things like application settings, database connection strings and the like could be set without having to manually update the web.config file (which could be overwritten) and could be deployed without having to manually update each of the front end servers.

    I tried the stsadm.exe -o copyappbincontent command to push my changes to the web.config and it worked. Unfortunately, it pushed the changes to all of the other SharePoint sites (MySite, SSP and Central Admin) which was an undesired affect. Also, even if that was acceptable the stsadm.exe -o copyappbincontent command would have to be run on every front end server.

    Any help you can provide would be appreciated.

    • Hi Randy,

      Did you get an answer for yourown question yet? I am at the same situation as yours. Wonder how can I get this additional config info added to web.config on existing webapp using wsp.

      If you have a solution, pls share with us.

      Thanks
      Firoz Ozman

  3. CJ said

    Hi Randy

    You can look at the following two articles to guide your solution

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

    http://paulhorsfall.co.uk/archive/2007/03/29/How-To-Programmatically-Disable-Custom-Errors-and-Enable-Debugging-And.aspx

    cheers

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

  5. [...] want to immediately apply the changes you have to run : stsadm –o copyappbincontentReference: http://claytonj.wordpress.com/2008/03/19/custom-webconfig-settings-in-sharepoint/3. See also : [...]

  6. Amol Bhosale said

    Hi Randy/Firoz,

    Did you get an answer for yourown question yet? I am at the same situation as yours. Wonder how can I get this additional config info added to web.config on existing webapp using wsp.

    If you have a solution, pls share with us.

    Thanks
    Amol Bhosale

  7. Laurence said

    I found that everything’s working as I expected by using option 2 and adding “stsadm -o copyappbincontent” as a ‘Post-deployment Command Line’ option in the SharePoint project in Visual Studio 2010. (Right click the project, select properties, select SharePoint at the bottom). The project also has the SharePoint Mapped Folder set to CONFIG with the webconfig.mySettings.xml in it, so it gets copied with every deploy.
    Hope it’s of help to someone.
    Laurence

  8. shankar narayanan said

    What the hell is this man? Damn it…

  9. shankar narayanan said

    What the hell is this man? Damn it…

    shankar.narayanan@tcs.com

  10. jyothsnatadikonda@gmail.com said

    WF

  11. ahcouturier@gmail.com said

    eyy dude, i need some help, well im using sharepoint server 2010 with reporting services, the situation here is, when i used a domain count thats has permission in reporting services as viewer, no have any problem, but when i log into sharepoitn with a domain coun but without permision of viewer on reporting services its send me an error of permision.

    I was thinking edit the webconfig of sharepoint to assing as a key the user and password of a count of viewer, then everithing could view the reports displayed.

    tell me this is posible ?? or exist another way to assign this domain count on a global nivel over sharepoint ??

  12. learn said

    learn…

    [...]Custom web.config settings in SharePoint « CJ[...]…

  13. nando pacheco…

    [...]Custom web.config settings in SharePoint « CJ[...]…

  14. Before you make a determination where real-estate dealer to use,
    observe how they negotiate. Check with what kind of instruction and expertise they have.

    You should also review the ways they prefer and make certain they are ethical
    when you are performing organization. Question to view
    the broker’s collection. He should certainly provide you instances of effective talks. Also request the broker to give you an illustration of an not successful negotiation and make clear what he discovered in the encounter.

  15. Sarvesh Vinod said

    I created a new sharepoint application. I connected db using sqladapter. How can I connect db using web.config in sharepoint? How can I implement in my site?

  16. When you rent a corporate apartment in Melbourne,
    you are assured of the best service. However, these apartments can be rented for minimum 7 days.
    Are you thinking of buying property in London as a
    pied a terre or an investment property in London. With corporate travel
    soaring inexorably, the latest entrant in the Indian hospitality industry has earned the plaudits of
    the business community primarily because of its inimitable
    comfort and privacy.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 26 other followers

%d bloggers like this: