none
How hard is to build an InfoPath form programatically? RRS feed

  • Question

  • Hi Everyone,
    I need to investigate how hard is to build an InfoPath form programatically. I've found some stuff about how to create an InfoPath form using an XSN file (http://www.kindohm.com/archive/2008/01/10/programmatically-create-an-infopath-form-instance-from-xsn-template.html) but this is not what I want. I have to generate programatically all files created by InfoPath when you choose "Save as Source Files" option. So, in fact I'll be creating the template for the form.
    I also found this question on StackOverFlow: http://stackoverflow.com/questions/926733/programatically-generate-infopath-form-template
    Has anyone built something like this before? Do you think it will be like a "mission impossible" thing?
    Thank you very much!
    Bruno

    Bruno Nepomuceno
    Thursday, June 2, 2011 11:32 PM

Answers

  • Dear Bruno,

    I think this article may help you:

    Programmatically create an InfoPath form from scratch using a Console application and C# code

    http://www.bizsupportonline.net/infopath2007/programmatically-create-infopath-form-console-app.htm

     

    byte[] infoPathFormData = null;
    
    using (MemoryStream ms = new MemoryStream())
    {
    using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8))
    {
    // Define the namespace for the form
    string myNamespace = "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-09-02T01:11:44";
    
    writer.WriteStartDocument();
    
    // Create the required processing instructions
    writer.WriteProcessingInstruction(
    "mso-infoPathSolution", 
    "name=\"urn:schemas-microsoft-com:office:infopath:ProgCreateForm:-myXSD-2007-09-02T01-11-44\" solutionVersion=\"1.0.0.2\" productVersion=\"12.0.0.0\" PIVersion=\"1.0.0.0\" href=\"http://servername/formlibname/forms/template.xsn\"");
    writer.WriteProcessingInstruction(
    "mso-application", 
    "progid=\"InfoPath.Document\" versionProgid=\"InfoPath.Document.2\"");
    
    // Create the XML of the main data source of the form
    writer.WriteStartElement("my", "myFields", myNamespace);
    writer.WriteStartElement("my", "field1", myNamespace);
    // Fill the date field with today's date
    writer.WriteString(DateTime.Now.ToString("yyyy-MM-dd"));
    writer.WriteEndElement();
    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Flush();
    writer.Close();
    }
    
    infoPathFormData = ms.GetBuffer();
    ms.Close();
    }
    
    // Upload the newly created InfoPath form to SharePoint
    if (infoPathFormData != null && infoPathFormData.Length != 0)
    {
    using (WebClient client = new WebClient())
    {
    // Set the credentials to be used for upload to SharePoint
    client.Credentials = CredentialCache.DefaultCredentials;
    
    // Upload the newly created form to a SharePoint form library
    client.UploadData(
    @"http://servername/formlibname/newformname.xml", 
    "PUT", 
    infoPathFormData);
    }
    }
    

     

    Hope this helps.

    Regards,


    Be happy.
    • Marked as answer by Bruce Song Tuesday, June 21, 2011 1:55 PM
    Tuesday, June 7, 2011 8:31 AM