Is it possible to run C# code with InfoPath forms when the form is running inside of web browser?

Same question applies to JavaScript also. With web forms which one do you recommend if both are possible?


On 2007, while code-behind in InfoPath is possible, it means the form must be Administrator-approved and published through central administration. This is very tedious, and there are some lifecycle challenges (bugs) when it comes to deploying updates to administrator-approved forms once they are already in use.

After developing a lot of complex forms for workflow projects, my personal recommendation on 2007 is to avoid code-behind in forms like the plague. InfoPath integrates very niceley with web services, so I moved the code into custom web services and was always able to get away with not having code-behind.

On SharePoint 2010, InfoPath code-behind for browser forms runs as a sandbox-solution so the forms can be published directly to a site collection. The nice thing with the sandbox model in 2010 is that you have access to the form, and access to the sandbox SharePoint Object Model so you can read and write lists from anywhere in the site collection, etc. You are however limited by the inherent restrictions of the SharePoint sandbox.

JavaScript/JQuery opens up two choices: render the data down using something like the DataFormWebPart and XSLT and then spice it up with JavaScript, or use Ajax and make calls to the SharePoint Web Services. There is a great project on codeplex called SPServices that is a JQuery wrapper to the web services to make them a lot easier to use with JQuery.

Ultimately, the choice in my mind probably comes down to how complex the form is. InfoPath is very fast for developing some pretty complex forms (hidden sections, optional sections, repeating sections/tables, views, hierarchal data, etc). My recommendation is that as lonh as you can avoid administrator-approved forms, use InfoPath.

NOTE: InfoPath forms does require the Enterprise Edition CAL for SharePoint? A lot of people who might otherwise use InfoPath opt to go with other form approaches due to licensing concerns. Take that into consideration as well.

  • Hmmm... just realized you never mentioned 2007 in your original question. I picked up 2007 from the post by Rob. Are you running on SharePoint 2007 or 2010? – Chris Beckett Aug 7 '11 at 16:46
  • Thanks for the answer Chris. We run InfoPath 2010 on SharePoint 2010. – Élodie Petit Aug 8 '11 at 7:12
  • I would go InfoPath with sandbox code-behind as necessary. – Chris Beckett Aug 8 '11 at 15:29

Both are possible in InfoPath 2007, but support for scripting was removed, at least from the design-time experience (...I believe script in 2007 forms will still run when upgraded to 2010), in SharePoint 2010.

In 2010, you can attach C# code to form and control events. Here's a good lab from MSDN: http://msdn.microsoft.com/en-us/library/gg435971.aspx

  • But these events are not available in browser-enabled forms, right? – Élodie Petit Aug 8 '11 at 7:11
  • The event model is available for browser-enabled forms...I can't give you a more specific answer to your very broad question. – Rob D'Oria Aug 8 '11 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.