How to create a new Form from Template in a Form Library, programmatically.

by Daniel Halan 31. July 2007 03:38

Creating new files in a Document Library is a pretty simple task as copying the template located in the "Forms" folder. But creating a new Form is a bit harder as the template is a binary file, and what we want is a new Xml file.

The template file (with extension XSN) created by InfoPath is actually a CAB archive, that contains your form logic compiled, schemas and your xml template. So what you have to do is extract the CAB file, and copy the "template.xml" file to your Form Library. You can find a CAB extractor at The Code Project.

Here is a example of a method to get the template Xml:

private byte[] GetXmlForm(SPDocumentLibrary list) {
  byte[] data = null;
  SPFile file = list.ParentWeb.GetFile(list.DocumentTemplateUrl);
  Extract cab = new Extract();
  string szFolder = string.Concat(System.IO.Path.GetTempPath(), list.Title, "\\");

  if( !Directory.Exists(szFolder) )
    Directory.CreateDirectory(szFolder);

  cab.ExtractStream(file.OpenBinaryStream(), szFolder);
  FileStream fs = new FileStream(szFolder + "template.xml", FileMode.Open);
  try {
    data = new byte[fs.Length];
    fs.Read(data, 0, data.Length);
  } finally {
    fs.Close();
  }
  return data;
}


There is one more thing that has to be done before saving the template, which is to add a hyperlink to your InfoPath Template. The href should be added in the processing instruction tag "mso-infoPathSolution" ie.
<?mso-infoPathSolution href="http://server/site/library/Forms/myTemplate.xsn" ?>

Here is a snippet on how to do it:

frm.SetTemplateUrl("http://Server/Site/{0}"+list.DocumentTemplateUrl);

public
void SetTemplateUrl(string url) {
  foreach(XmlNode n in m_Doc.ChildNodes) {
    if(n.Name == "mso-infoPathSolution") {
      string szHref = string.Format("href=\"{0}\"", url);
      Regex r = new Regex("href=\".*\"");
      if(r.IsMatch(n.Value))
        n.Value = r.Replace(n.Value, szHref);
      else n.Value = string.Concat(n.Value,szHref,' ');
    }
  }
}


 

Comments

11/8/2009 1:22:32 AM #

Nardu

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

Nardu United States | Reply

4/16/2010 10:59:21 PM #

soumyendra

Hi,

I am really thankful for your post. I am able to extract the Template.xsn file and submit a xml file in the form library programmatically.

Now, I am getting an error/warning, when I browse the SharePoint form library, and try to open the file.
"The form template associated with this form cannot be found. It is not your computer and the form does not specify a location from which to retrieve it."

Please let me know, if you also faced the same.

soumyendra United States | Reply

4/16/2010 11:01:17 PM #

soumyendra

Hi,

The above error I am getting because the template source is different from the template already attached in the library.

Thank,
Soumyendra  

soumyendra United States | Reply

4/19/2010 4:18:22 PM #

soumyendra

Hi,

Thanks it's worked now, actually I missed the later part of the solution "mso-infoPathSolution" section.

Thanks,
Soumyendra

soumyendra United States | Reply

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.1.2
Theme by Daniel Halan

About the author

Daniel Halan Daniel Halan systems architect at Logica in Sweden.

Working primary with Microsoft Dynamics CRM.

Complete Profile

The content of this site are my own personal opinions and do not represent my employer's view in anyway.

Follow me on Twitter