Microsoft InfoPath 2010
The official blog of the Microsoft InfoPath team

How to Enqueue Submits While Offline

How to Enqueue Submits While Offline

Rate This
  • Comments 12

How to Enqueue Submits While Offline

Applies to: Microsoft Office InfoPath 2003 SP1

Scenario:

User fills out forms for a particular form template while the laptop is disconnected from the network.  Upon reconnection, seamlessly syncrhonize the offline forms to a Windows SharePoint Services form library. 

Solution:

Using OnSubmitRequest, you can have the code save to the offline folder depending on if we're offline or not, as in the following code, which is written in C# using our Visual Studio .NET Toolkit.  You will also need to add a project reference to System.Xml.dll and the using System.XML; directive to your form code.

  public void OnSubmitRequest(DocReturnEvent e)

  {
 
    if (thisApplication.MachineOnlineState == XdMachineOnlineState.Online)
    {
     // submit to Sharepoint using DAVAdapter
     (thisXDocument.DataAdapters["Submit to Sharepoint"] as DAVAdapter).Submit();
    }
    else
    {
     // We are offline or working offline.
     XmlDocument oDOM = new XmlDocument();
     Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMDocument oWrappedDOM = thisXDocument.DOM;
 
     oDOM.PreserveWhitespace = true;
     oDOM.LoadXml(oWrappedDOM.xml);
     oDOM.Save("C:\\Submit\\Form_"
            + DateTime.Now.ToString("yyyy.MM.dd_HH.mm.ss.ff") + ".xml");
    }
  
    e.ReturnStatus = true;
  }
 

Then, if you've been offline for a while and a number of files have built up, you can run the following jscript to invoke InfoPath externally and force a submit looping through the files, as below:

 
var oFileSys = new ActiveXObject("Scripting.FileSystemObject");
var oSubmitDir = oFileSys.GetFolder("C:\\Submit");
var oFilesInSubmitDir = new Enumerator(oSubmitDir.Files);
 
var oApp = new ActiveXObject("InfoPath.Application");
 
var fso = new ActiveXObject("Scripting.FileSystemObject");
 
for (; !oFilesInSubmitDir.atEnd(); oFilesInSubmitDir.moveNext())
{
 var strFileName = oFilesInSubmitDir.item().Name.toLowerCase();
 
 if (strFileName.length >= 4
     && strFileName.lastIndexOf(".xml") == (strFileName.length - 4))
 {
  try
  {
   var oDoc = oApp.XDocuments.Open(oFilesInSubmitDir.item().Path);
   oDoc.Submit();
   oDoc.View.Window.Close();
   
   var f = fso.GetFile(oFilesInSubmitDir.item().Path);
   f.Delete();
 
  }
  catch (e)
  {
   WScript.echo("ERROR: " + e.description);
  }
 }
}
oApp.Quit();
 
 
Leave a Comment
  • Please add 2 and 2 and type the answer here:
  • Post
  • ??????? InfoPath ?????????
  • Tried your code and it works great...but...when I close the form, it doesn't prompt me to save the changes when the form data has been changed (i.e. dirty).

    Do you know if there is another event to check to see if the form is dirty before exiting?

    Thanks
  • The document dirty state can be read from the XDocument.IsDirty property. This property can be programmatically set with the XDocument.SetDirty(boolean) method.
  • Is there a way to use a web service as a secondary data source, and then when disconnected, use a cached version of the data that was available when the form was received?
  • I have the same need for using offline cached secondary data sources. I am interested in a response to Christoper's question.
  • Rob / Christopher - We don't have a sample readily available that we can post for that (but we'll consider that for future versions). In the meantime, please try asking the newsgroup (http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.infopath )
  • I wrote an aspx page that responds with simple XML documents. I then set up a secondary data source that "reads" the aspx file via a URL. It worked! Not only that, I can pass query string elements, and the data that is downloaded is somehow remembered by Infopath when the system is offline. I believe the secondary data source is stored in the temporary internet files, because clearing this folder kills the Infopath template cache and the copy of the SDS data.
  • I can't for the life of me get this to work with InfoPath 2010. Could anyone provide a tutorial or short walkthrough?

    Thanks in advance.

  • Hi plavix,

    The code for InfoPath 2007 or 2010 is significantly shorter:

    public void FormEvents_Submit(object sender, SubmitEventArgs e)

    {

    try

    {

    if (Application.MachineOnlineState == MachineState.Online)

    {

    this.DataConnections["SharePoint Library Submit"].Execute(); //Assumes your submit connection is named as shown

    }

    else

    {

    string strSaveLoc = @"C:\Submit\Form_";

    this.SaveAs(strSaveLoc + DateTime.Now.ToString("yyyy.MM.dd_HH.mm.ss.ff") + ".xml");

    }

    e.CancelableArgs.Cancel = false;

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message);

    e.CancelableArgs.Cancel = true;

    }

    }

    With the above code, if I am offline, the form "submits" (saves) to the local machine location I specified above. I can then open that form when I am back online and it will submit to my SharePoint library.

    Scott

  • Hi can the same code be used in infopath 2007.

  • Hi Heerachand,

    The only way this will work in InfoPath 2007 and later is if you change the code to be InfoPath 2003 Compatible. The object model changed with 2007 so if you want to use the later object model you will need to re-write this code using the new methods, properties, etc.

    Scott

Page 1 of 1 (12 items)