Sending an InfoPath 2003 form to people who don’t have the client

February 7, 2007

I needed to send the completed (ie approved) form, read-only, to the company who took care of the orders made in an InfoPath 2003 form. The form is stored in a SharePoint forms library, but the receiving company didn’t have access to this SharePoint site. 2007 would be sweet, but I can’t use that. Hmmm… how to do?

I created a web service, which the form, once approved, sent an email through (all stages in the workflow before approval used the ordinary data connection to the SharePoint forms library).

The web service receives the xml document from InfoPath, via the normal web service data connection available. The web service extracts the filename (stored as a secondary data source inside the form) and makes an html document out of the xml and xsl combined, which then can be viewed on any computer with an internet browser.

There’s one big-ass downside with this though. The xsl file has to be extracted from the infopath form and then edited a little to remove not-supported functions and so on, or else you’ll se some nasty error messages when opening the form in the browser. So if the form has undergone some major changes, you need to re-extract the xsl-file and put it somewhere where the web service can access it, which in my case is in the web service root directory. 

Here’s the code:

Service.cs


[WebMethod]
  public string InfoPathMailer(XmlDocument xmlDoc)
  {
    string filename = GetFileName(xmlDoc);
Stream stream = new MemoryStream();
    XslCompiledTransform xslTransform = new XslCompiledTransform();
    xslTransform.Load(Server.MapPath("view1.xsl"));
    xslTransform.Transform(xmlDoc, null, stream);
    stream.Position = 0;
MailSender m = new MailSender("yourmailserver.net", "formsrobot@yourdomain.com", "receiver@company.com", "New End User Request", "Do not reply to this address", filename, stream);
    m.Send();
    return "";
  }
private string GetFileName(XmlDocument xmlDoc)
  {
    string infoPathNamespacePrefix = "my";
    string infoPathNamespaceURI = @"http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-11-13T10:36:39";
    XmlNamespaceManager namespaceMngr = new XmlNamespaceManager(xmlDoc.NameTable);
    namespaceMngr.AddNamespace(infoPathNamespacePrefix, infoPathNamespaceURI);
    XmlNode node = xmlDoc.SelectSingleNode("//my:Filename", namespaceMngr);
    string s = node.InnerText;
    s = s.Replace("xml", "html");
    return s;
  }

And here’s the code for the class MailSender:


using System;
using System.Data;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
public class MailSender
{
  MailMessage mail;
  String smtpServer;
  //Constructor for attachment as stream
  public MailSender(string server, string sender, string receiver, string subject, string body, string filename, Stream stream)
  {
    smtpServer = server;
    mail = new MailMessage(sender, receiver, subject, body);
    Attachment attachment = new Attachment(stream, filename, MediaTypeNames.Text.Html);
    mail.Attachments.Add(attachment);
  }
public void Send()
  {
    SmtpClient client = new SmtpClient(smtpServer);
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    client.Send(mail);
  }
}

Hopefully this can help someone out there. Good luck!

3 Responses to “Sending an InfoPath 2003 form to people who don’t have the client”

  1. Ben Says:

    This is very nice code, and real helpful. I have been battling InfoPath (’07) and Web Services to produce a POC Solution. Here goes: We have a web service that needs to send out InfoPath (07) emails to designated users.

    The template we are using has input parameters to pop the data for each specific user. The problem is I cannot , for the life of me, figure out how to programmatically format the email so the infopath.xml form is expressed in the email as it does when you send it via the “send this form to recipients”.

    I attach the file as disposition.Inline = true. But I just receive a regular attachment where you have to double click the .xml file and it opens in infopath (this is what we’re trying avoid).

    Is there any way to programmatically send InfoPath forms with its correct formatting? If you have any thoughts I would really, REALLY appreciate it

  2. microserf Says:

    Hello Ben,

    I’m sorry but I never managed to format my outgoing mail messages to anything other than plain text. I didn’t try very hard either, because we wanted the files to be sent as attachments (for archiving reasons). So I’m of no help there.

    Good luck!
    // Peter

    • Prem Says:

      Hi Ben,
      I have the exact same situation, I can see this was posted long back. By this time if you had managed to have any solution. Can you please share it with me?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: