Search code examples
sharepointsharepoint-2010infopathinfopath2010

How to programmatically create an InfoPath form from an InfoPath XSN template


I need a solution that creates an InfoPath instance form from an XSN template that exists on a SharePoint server, I am using this approach but this extracts template files on temp directory of server that we may not have write permission to. Is there better solution for this?


Solution

  • You just change the CAB-library, to one that can extract the template file to memory, as this one,

    Minimum C# code to extract from .CAB archives or InfoPath XSN files, in memory

    And then call, myCab.ExtractFile("template.xml", out buffer, out bufferLen);

    the complete code would look something like

    private byte[] GetXmlForm(SPDocumentLibrary list) {
      byte[] data = null;
      SPFile file = list.ParentWeb.GetFile(list.DocumentTemplateUrl);
    
    
      Stream fs = file.OpenBinaryStream();
      try {
        data = new byte[fs.Length];
        fs.Read(data, 0, data.Length);
      } finally {
        fs.Close();
      }
    
      byte[] buffer;
      int bufferLen;  
      CabExtract cab = new CabExtract(data);
      cab.ExtractFile("template.xml", out buffer, out bufferLen);
    
      return buffer;
    }