Open an InfoPath File from a Web Page - Agni Reddy
in

InfoPath Dev

Agni Reddy

Open an InfoPath File from a Web Page

There are times when you get data from a DB and want to throw an xml file from a web page. To do that, in your aspx code you would have to build the InfoPath xml with the PI information using methods in the System.Xml namespace (or StringBuilder String.Format etc). This xml string is then given as input to the SendContent function shown below. This function should also be added to the code running behind the aspx page.

public static void SendContent(string content)
{
    if(content == null || content == String.Empty)
        return;

    Response.Clear();
    byte []bytes = Encoding.UTF8.GetBytes(content);
    Response.AddHeader("Content-Disposition", "attachment; filename=MyFileName.Xml");
    Response.ContentType = "text/xml"; 
    Response.AddHeader("Content-Length", bytes.Length.ToString());
    Response.Write(content);
    Response.End();
}

This will throw up a dialog box with the file name specified in the filename=MyFileName.Xml where the user will be asked to either Open, Save or Cancel. When Open is clicked the file will open InfoPath using the solution PI provided. But the code above shows a regular xml icon in the Open/Save/Cancel dialog box. If you want to change this to show an InfoPath Icon instead change the following line of code:

Response.AddHeader("Content-Disposition", "attachment; filename=MyFileName.Xml");

As follows:

Response.AddHeader("Content-Disposition", "attachment; filename=MyFileName.infopathXml");

You can also change the file name to be more pertinent by getting some data from the xml string and putting that information instead of MyFileName.

Comments

No Comments

About Agni Reddy

Agni is a Microsoft MVP. He began his InfoPath career in 2004 testing solutions developed at Qdabra™ Software and quickly transitioned to an InfoPath developer. He now has expertise developing complex InfoPath solutions. Agni holds a Master's degree in Information Systems.
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.