Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

Accessing configuration settings from InfoPath 2007 browser-enabled form templates


Author: S.Y.M. Wong-A-Ton MVP

View products that this article applies to.

Summary

Learn how to access custom configuration settings that are stored in Windows SharePoint Services web application configuration files from InfoPath browser-enabled form templates.

↑ Back to the top


Abstract

This article discusses how to access custom configuration settings�that are stored in web configuration files from InfoPath browser-enabled form templates. It assumes that you are familiar with configuration files, the logical architecture of Windows SharePoint Services 3.0�(WSS), InfoPath Forms Services, and writing .NET code for InfoPath form templates.

↑ Back to the top


Introduction

InfoPath browser-enabled form templates are InfoPath form templates�that have been designed to be compatible with and run in both the InfoPath client application and on a server running�InfoPath Forms Services. InfoPath forms�that are based on such form templates can be opened in and filled out through a web browser such as Internet Explorer. These InfoPath forms run within the context of a Windows SharePoint Services 3.0�(WSS) site or site collection. Each WSS site or site collection belongs to an WSS web application and each WSS web application uses a web configuration file. Therefore, an InfoPath form�that runs in the context of an WSS�site or site collection has access to settings that are stored in the web configuration file of the WSS web application of that site or site collection.

↑ Back to the top


Configuration settings

Configuration files are XML files that can be changed as needed and allow for flexible configuration of applications, either desktop or web. WSS web applications are physically stored in folders under the C:\Inetpub\wwwroot\wss\VirtualDirectories directory. Each web application has its own folder. The configuration (web.config) file for a web application is located in the root folder for the web application. You can add custom settings to this web configuration file, which InfoPath browser-enabled form templates can access and use.

Configuration settings can be programmatically accessed from an InfoPath form template just like from any other .NET application by using classes and methods defined in the System.Configuration namespace. First you must reference the System.Configuration assembly in your InfoPath project in Visual Studio or Visual Studio Tools for Applications (VSTA) and then you can write code to retrieve the values of custom configuration settings from the configuration file as shown in the following code:


Visual Basic
Dim�appName As String = System.Configuration.ConfigurationManager.AppSettings("AppName").ToString()

C#
string�appName = System.Configuration.ConfigurationManager.AppSettings["AppName"].ToString();


where the appSettings section in the configuration file�has an add element�that defines�a custom�application setting named AppName�with a value of InfoPath2007 as�follows:

<appSettings>
� <add key="AppName" value="InfoPath2007" />
</appSettings>


Once you have retrieved the value of�the custom application setting from�the configuration file, you can use it in your�InfoPath form template to dynamically configure default values of list boxes, show or hide sections based on conditional formatting, pre-fill fields with data, etc.

↑ Back to the top


Testing and debugging

Typically, you must publish an InfoPath browser-compatible form template to a server running InfoPath Forms Services and browser-enable it before you can debug it in Visual Studio. And since you must write .NET code to access configuration settings, you cannot publish the InfoPath form template directly to the server; an administrator must approve and upload it to the server. However, you can avoid having to publish an InfoPath form template to a server running InfoPath Forms Services for the sole purpose of testing and debugging code�that accesses configuration settings.

Whenever you preview an InfoPath form, the InfoPath client application, INFOPATH.EXE, is executed to be able to open and display the InfoPath form in Preview mode. This applies to both Visual Studio and Visual Studio Tools for Applications (VSTA). So if you want an InfoPath form template to access configuration settings while you are debugging it, you must create a local application configuration file�that INFOPATH.EXE can access and use.

Perform the following steps to create an application configuration file for INFOPATH.EXE:
  1. Open Windows File Explorer and go to the directory where INFOPATH.EXE was installed. By default InfoPath 2007 is installed in C:\Program Files\Microsoft Office\Office12.
  2. Create a new text file in the same directory as INFOPATH.EXE and name it infopath.exe.config.
  3. Open the newly created file in Notepad and add the desired application configuration settings to it. These settings should resemble the settings that you will use later on in the�WSS web application configuration file that your InfoPath browser-enabled form template�will be accessing. See listing 1 for an example of the contents of a simple configuration file.
  4. Save and close the file.

Now you can set breakpoints throughout your code and run and debug the code�that accesses configuration settings.

Listing 1. Sample contents of infopath.exe.config file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
� <appSettings>
��� <add key="AppName" value="InfoPath2007" />
� </appSettings>
</configuration>


↑ Back to the top


Conclusion

InfoPath forms�that are based on InfoPath browser-enabled form templates can access web configuration settings of the WSS site or site collection they are opened in. You can use classes and methods defined in the System.Configuration namespace to write code�that accesses these configuration settings from within InfoPath form templates. You can create a temporary application configuration file for the InfoPath client application to use, so that you can test and debug configuration code without first having to publish an InfoPath browser-compatible form template to a server running InfoPath Forms Services and browser-enable it.

↑ Back to the top


Properties

COMMUNITY SOLUTIONS CONTENT DISCLAIMER
MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

↑ Back to the top


Community solutions content disclaimer

Microsoft corporation and/or its respective suppliers make no representations about the suitability, reliability, or accuracy of the information and related graphics contained herein. All such information and related graphics are provided "as is" without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information and related graphics, including all implied warranties and conditions of merchantability, fitness for a particular purpose, workmanlike effort, title and non-infringement. You specifically agree that in no event shall Microsoft and/or its suppliers be liable for any direct, indirect, punitive, incidental, special, consequential damages or any damages whatsoever including, without limitation, damages for loss of use, data or profits, arising out of or in any way connected with the use of or inability to use the information and related graphics contained herein, whether based on contract, tort, negligence, strict liability or otherwise, even if Microsoft or any of its suppliers has been advised of the possibility of damages.

↑ Back to the top


Keywords: KB555963, kbhowto, kbpubtypewp, kbpubtypecca, kbpubmvp

↑ Back to the top

Article Info
Article ID : 555963
Revision : 1
Created on : 9/26/2007
Published on : 9/26/2007
Exists online : False
Views : 556