Writing to the Windows Event Log from an InfoPath browser form

If you're writing code in VSTA for an InfoPath browser form, want to debug it, but do not have Visual Studio, you can use the workaround described in this post.

In a previous post I talked about how you can use writing values from code in an InfoPath browser form to the Windows Event Log as a workaround for not having Visual Studio to be able to debug a browser-enabled form template in SharePoint.

The only caveat is that you’ll have to give the InfoPath form template Full Trust to be able to write to the Windows Event Log.

If you don’t do this, you may get the following error message:

Request for the permission of type ‘System.Diagnostics.EventLogPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ failed.

The code for writing to a Windows Event Log on the SharePoint server where the InfoPath browser form is opened, is pretty straightforward.

After adding a reference to the System.Diagnostics namespace, you can write code similar to the following:

string source = "InfoPath Browser Form";

if (!EventLog.SourceExists(source))
{
EventLog.CreateEventSource(source, "Application");
}

EventLog log = new EventLog("Application");
log.Source = source;
log.WriteEntry("Your message goes here.", EventLogEntryType.Information);

Another workaround similar to writing to the Windows Event Log would be writing to a text file on the SharePoint server for debugging purposes.

Related Posts

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. Usage of techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

InfoPath 2010 Cookbook

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010