Programmatically Configure commerce server Sitename in web.config file using SPWebconfigmodification


There are two different ways to configure commerce server with sharepoint site.
  1. Through commerce server Tool -Microsoft Sharepoint commerce service configuration wizard
  2. By activating commerce server feature on sharepoint site (using site definition).
So here I have followed 2nd approach. 

Once commerce server site is created using tool site packager, few commerce servers' related features get available under 12 folder, from those features if you activate feature named "CommerceServer" on sharepoint site. It copies few files Channel.config, MetadataDefinitions.xml, MetadataDefinitions.xml etc under virtual directory of sharepoint site. Also it modifies the web.config of the site.

In the modified web.config file we have to integrate commerce server site name. So instead of manually updating commerce server site name we can programmatically configure it using sharepoint webmodificator. Created one feature and for that feature written one feature receiver. On FeatureActivated event write a following code 

There are three places in web.config where we need to place the "SiteName". 

// commerce server Site Name which you can find under commerce server manager
var sitename = "MySite" 
string modificationOwner= "configmodificator";

Here is the web.config file node and code to place site name at proper position in the node.

You can find following line in web.config file 

//Add the sitename
<application  siteName="" debugLevel="Production"></application>

Code to add sitename using SPWebconfigmodification as

Here we have to place value of "sitename" attribute of application node. So webconfig modification type "EnsureAttribute" is used.  

Here you can find another place in web.config where sitename need to be configured

// Add sitename next to the Commerce Server 2007 Keys
<keys keyIndex="1">
 <add type="publicKey" value="registry:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Commerce Server 2007 Keys\,PublicKey"> </add>
 <add type="privateKey1" value="registry:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Commerce Server 2007 Keys\,PrivateKey"> </add>
 <add type="privateKey2" value="" />
</keys>

Here is the code to add sitename for above example 

1.gif

webApp is object of current SPWebApplication. So add modification, mod2 and mod3 into WebConfigModifications

webApp.WebConfigModifications.Add(modification);
webApp.WebConfigModifications.Add(mod2);
webApp.WebConfigModifications.Add(mod3);

Finally apply webconfig modification and update the webapplication as 

2.gif

Also on FeatureDeactivating event we can remove the changes made in web.config as follows-

If you keep same owner name like here we are using modificationOwner so we can get all the SPWebConfigModifications objects. 

Code to remove the changes made in web.config as follows-

var modificationItems = (from SPWebConfigModification modificationItem in webApp.WebConfigModifications where modification.Owner == modificationOwner
select modification).ToArray();
foreach (var modificationItem in modificationItems)
{
    webApp.WebConfigModifications.Remove(modificationItem);
}

Up Next
    Ebook Download
    View all
    Learn
    View all