During the last project I have been involved in developing
several custom forms for SharePoint 2010 in InfoPath 2010.
During this task I have discovered and learn on my own skin many
products limitation and the different approach in the way on how
InfoPath works. I believe that this small guide should provides you
few tricks that believe me, are good to be read before doing any
dealings with InfoPath.
During the last project I have been involved in developing
several custom forms for SharePoint 2010 in InfoPath 2010.
During this task I have discovered and learn on my own skin many
products limitation and the different approach in the way on how
InfoPath works. I believe that this small guide should provides you
few tricks that believe me, are good to be read before doing any
dealings with InfoPath.
Let's imagine that you today's task is to create a simple
InfoPath form which
- it sets and displays /hides few controls,
- displays a message box,
- based on some external parameters
- allows the user to submit it by pressing a button.
Looks like a pretty simple task.. right? Actually.. not
really if it is your first fight against InfoPath.
Let's see first what you should keep in mind, before starting any
coding:
- InfoPath 2010 crashes easily. Save often.
- Custom code in InfoPath 2010 can be only written using
Visual Studio 2008. And yes... if you are using Visual Studio 2010,
you need to install also the 2008 version.
- Be aware if you are using any source control program that uses
read-only lock like TFS or SourceSafe. This programs locks in
read-only your .csproj and InfoPath will complain
-

Now let's see how can we accomplish those 3 simple tasks..
1) How can I hide or show my controls
programmatically?
You cannot!
The only possible workaround is using panels that you can
show or hide based on a condition.
- add a panel
- insert in the panel a checkbox
- select the panel and under the Control Tools tab , click Manage
Rules.
- add a formatting rule which hides the control if the
checkbox is checked

Now you can control the panel's visibility by setting the
checkboxes value. Set it to false to hide the panel.
1-b) And how can I set or get controls
values programmatically?
Here is a small code snippet that would helps you in setting and
reading your controls values
private void AppendFieldValue(string xPath, string xValue, bool isNi)
{
string oldValue = GetFieldValue(xPath);
SetFieldValue(xPath, String.Concat(oldValue, xValue), isNi);
}
private void SetFieldValue(string xPath, string xValue, bool isNil)
{
try
{
XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();
XPathNavigator xnMyField = xnDoc.SelectSingleNode(xPath, this.NamespaceManager);
if (isNil)
{
if (xnMyField.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))
xnMyField.DeleteSelf();
}
xnMyField.SetValue(xValue);
xnDoc = null;
xnMyField = null;
}
catch (Exception ex)
{
throw new Exception(xPath + ":" + xValue + ":" + ex.StackTrace, ex);
}
}
private string GetFieldValue(string xPath)
{
XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();
XPathNavigator xnMyField = xnDoc.SelectSingleNode(xPath, this.NamespaceManager);
if (xnMyField != null)
{
return xnMyField.Value;
}
else
{
return string.Empty;
}
}
2) Can I create a message box to alert users in infopath
2010?
No, you cannot. You have to play a little with Infopath Views.
Create a view wich contains your text message and just switch
to the view whenever you need to print the message.
this.ViewInfos.SwitchView("YOURVIEW");
3) Can I pass any parameter to my custom form when I
render it in SharePoint through FormServer.aspx?
Yes. The best way to accomplish that is using querystrings. Call
the FormServers.aspx by passing the querystring and get the value
from code in the FormEvents_Loading event. Here is and example:
call you form by passing a a querystring:
http://yourServer/_layouts/FormServer.aspx?XsnLocation=~sitecollection/FormServerTemplates/YourTemplate.xsn&OpenIn=Browser&SaveLocation=http://yourserver/yourDocLib&
myQueryStringValue=helloWord
And add this snippet into the FormEvents_Loading event:
public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
try
{
if (e.InputParameters.ContainsKey("myQueryStringValue"))
string myQueryStringValue = e.InputParameters["myQueryStringValue"];
}
catch (Exception ex)
{
//do something
}
}
4) How to programmatically submit your form to document
library?
First create a new data connection in Infotpath 2010 as a Submit
and To a Document Library on a SharePoint site. Don't't worry in
entering any SharePoint Server's url and destination document
library, because this values can be overridden in code. Once you
have created the new Data Connection, name it as SharePoint Library
Submit.

Now add a simple button, and in the buttons propery windows
select Action= Submit, press Submit Options and check Perform
Custom action using Code and finally press Edit Code.

Add the following code in the FormEvents_Submit event
public void FormEvents_Submit(object sender, SubmitEventArgs e)
{
try
{
FileSubmitConnection spConn = DataConnections["SharePoint Library Submit"] as FileSubmitConnection;
spConn.FolderUrl = "http://yourServer/yourDestinationDocLibUrl/"l;
spConn.Filename.SetStringValue("YourFileName.xml");
spConn.Execute();
e.CancelableArgs.Cancel = false;
}
catch (Exception ex)
{
e.CancelableArgs.Cancel = true;
}
}
I hope this small guide helps you in this basic tasks when
developing custom forms in Infopath 2010 for SharePoint 2010.