Saturday, September 25, 2010

Add system.serviceModel to web.config

Share/Save/Bookmark

Here is the code built on the Sharepoint API that will add WCF/web service configuration, system.serviceModel to web.config.


            SPWebConfigModification item = new SPWebConfigModification();
            item.Name = "system.serviceModel";
            item.Value = " <>... ";
            item.Path = "configuration";
            item.Sequence = 0;
            item.Owner = "owner";
            item.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            try
            {
                SPWebApplication parent = properties.Feature.Parent as SPWebApplication;
                if (parent != null)
                {
                    parent.WebConfigModifications.Clear();
                    // To know why i have used Clear(), read this
                    parent.Farm.Services.GetValue().WebConfigModifications.Clear();
                    parent.WebConfigModifications.Add(item);
                    parent.WebService.ApplyWebConfigModifications();
                    parent.Update();

                }
            }

Same code can be used to add any entires to web.config by giving respective path.
To know other approaches of modifying web.config read Ways to Modify web.config

  Subscribe

Tuesday, September 14, 2010

Ways to modify web.config

Share/Save/Bookmark


All about modifying web.config in Sharepoint environment.

Following are the different approaches that i have figured out that can possibly be used to modify web.config:

1. Rich API SPWebConfigModification
2. Declarative approach by having the changes in webconfig.*.xml
3. using ASP.net configuration
4. Modifying it as just another xml file

1. SPWebConfigModification

Sharepoint api is compiled so well that the changes that are written to web.config
can be easilty removed as well. A code sample to add the WCF/webservice entry using this API can be found here.

2.Declarative approach

When we are so used to do everything as a xml file. Ofcourse its the easiest way of
implementing things in sharepoint and so in this case.

In this approach the modifications to the web.config can be defined in a XML file
that will named as webconfig.*.xml,placed in 12\config folder.
When a  new webapplication is getting created sharepoint looks up for the files with pattern webconfig.*.xml
under 12/config and merges those changes with the web.config
Please note that this works only for new webapps and it impacts all webapps but not a specific
webapp.

To have these new changes to be included in web.config for existing webapp the following stsadm command
can be run:
stsadm -o copyappbincontent

3..Net Configuration API
All the elements/nodes and attributes of web.config are implemented as various classes and properties.
This will be a large chunk of code once you are used to SPWebConfigModification API.
Below is the code sample using Configuration class that will modify a entry in appSettings section.

public void Modify(string key, string value)
{

Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");

AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");

   if (appSettingsSection != null)

   {

       appSettingsSection.Settings[key].Value = value;

       config.Save();

   }

}


4.As an xml file.
Best way to parse the xml files is to use XMLReader/XMLTextReader,XMLWriter/XMLTextWriter,XPathNavigator
but it would be a pain to write the changes as each node and attribute.
Again it depends on the requirement what to add/modify?

Sample code:

                XmlDocument document = new XmlDocument();
                document.Load(file);// file - web.config opened on FileStream

                XmlNode node = document.SelectSingleNode("/configuration");
                node.InnerXml = node.InnerXml + serviceModelChange;
                document.Save(file);

more details can be found at http://www.west-wind.com/Weblog/posts/2423.aspx


I summarize that SPWebConfigModification is the best/safe way of modifying web.config programmatically.

  Subscribe

Friday, September 10, 2010

SPWebConfigModifications throws exception all time

Share/Save/Bookmark


{Expression must evaluate to a node-set.}
{'' is an invalid expression}

Are you victim of the above error messages?
Is SPWebConfigModification throwing exceptions all the time when modifying the web.config?
And you think it should not give that message for the flawless code that you have written?
then root cause could be because of the corrupted entries in webconfigmodification collection that still persist.
So how to get rid of them?

Call SPWebApplication.WebConfigModifications.Clear() before adding the entries(SPWebApplication.WebConfigurations.Add(EntryItem))

In a very special case of mine the above call didn't succeed and i have to call
SPWebApplication.Farm.Services.GetValue().WebConfigModifications.Clear() too.

So what did we really do just now, The call SPWebApplicaiton.WebConfigModifications.Clear()
cleared the bad entries on the current webapp where we are trying to add the entries.But there
could be bad entries still existing on the other webapps because of which error might be thrown though
there is nothing wrong in your code.
So to clear out all the corrupted entries we have called SPWebApplicaiton.Farm.Services.GetValue().WebConfigModifications.Clear()
Internally this call will reload all the web.config entires and modifies the current webapp
web.config to include the new changes.


  Subscribe