Appvity Blog

Home of the SharePoint mobility topics

How to Programmatically Create an InfoPath Form from an InfoPath XSN Template

By Phong Dang posted June 24, 2013 05:26 | 1 Comments

By Phong Dang posted June 24, 2013 05:26 | 1 Comments

Let's say you design an InfoPath form and upload the associated form template into the SharePoint Central Administration site. What would you do if you wanted to create a new item in the Form Library based on this template? For example, I have a Form Library with the Project Information form and InfoPath template (Project Information.xsn) uploaded in the Central Administration site. These features are activated on the SharePoint site. I will create a new program that shows how to programmatically create an InfoPath form from an InfoPath XSN template. First, I create a new solution and name it Appvity.InfoPathFormSoln.  Next, I add two reference assemblies: Microsoft.Office.InfoPath.dll and Microsoft.Office.InfoPath.Server.dll, in order to use the InfoPath API.   Next, I add a new feature, Appvity.InfoPath, and set up the required information. Note: I do not want to activate this feature automatically so I set the Active On Default option to false. Next, I add a new event receiver for this feature. I do want to activate this feature, as my form library will update the InfoPath form and content type. Before implementing code for this event receiver, I create a new page, FormXmlParser.aspx. This page will help me read the Project Information.xsn template and generate it to template.xml. Note: Project Informaion.xsn is a cab file so you must extract the content before you can modify it. There are many methods to extract this file (such as: http://www.codeproject.com/Articles/15397/Cabinet-File-CAB-Compression-and-Extraction), but those methods do not work in SharePoint. Thus, I create an ASP.Net page. On this page, there is an instance of an XmlFormView control. An XmlFormView is an InfoPath Forms Services control that hosts an InfoPath form on any ASP.Net page (as long as you're running it in SharePoint). You can also host the XSN template, which is what this code will do. Note: Note the use of the OpenFileFromPackage method in the Initialize event handler. That is a method that will extract the template.xml file (or any other file) from the XSN. The template.xml file is a "blank" form used to create a new instance of an InfoPath form from a template. Next, to make sure this page works correctly, you want to deploy this project to a SharePoint server and browse this page: Back to event receiver class, I implement the FeatureActivated event. Now, you need to activate the Project Information content type in the form library and get the absolute path of the InfoPath template from the root web and then point it to your custom web page in the code and download the xml. After that, do whatever you want with the xml, and place it in a form library. public override void FeatureActivated(SPFeatureReceiverProperties properties) { try { string listName = "ProjectInformation"; string strContentType = "Project Information"; SPWeb web = properties.Feature.Parent as SPWeb; SPList formLibrary = null; try { formLibrary = web.GetList(web.Url + "/" + listName); } catch { } if (formLibrary != null) { SPContentType contentType = web.AvailableContentTypes.OfType<SPContentType>().Where(ct => ct.Name.Equals(strContentType)).FirstOrDefault(); if (contentType != null) { SPContentType infoPath = formLibrary.ContentTypes.OfType<SPContentType>().Where(c => c.Name.Equals(contentType.Name)).FirstOrDefault(); if (infoPath == null) { contentType = formLibrary.ContentTypes.Add(contentType); SPFolder rootFolder = formLibrary.RootFolder; //Add the content type back IList<SPContentType> ctList = rootFolder.ContentTypeOrder; ctList.Add(formLibrary.ContentTypes[strContentType]); rootFolder.UniqueContentTypeOrder = ctList; rootFolder.Update(); formLibrary.Update(); } else { contentType = infoPath; } } string templateUrl = contentType.DocumentTemplateUrl; SPFolder forms = web.GetFolder(web.Url + "/" + listName); if (forms != null) { WebClient client = new WebClient(); client.Credentials = CredentialCache.DefaultCredentials; if (templateUrl.LastIndexOf("/") != -1) { templateUrl = templateUrl.Substring(templateUrl.LastIndexOf("/")); templateUrl = "/FormServerTemplates" + templateUrl; } string fileInfoPath = web.Site.Url + templateUrl; Stream stream = client.OpenRead(web.Url + "/_layouts/Appvity.InfoPathForm/FormXmlParser.aspx?Url=" + fileInfoPath); StreamReader reader = new StreamReader(stream); string formXml = reader.ReadToEnd(); try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(formXml); UpdateInfoPathForm(xmlDoc); Stream outStream = new MemoryStream(); xmlDoc.Save(outStream); SPFile file = forms.Files.Add(formLibrary.Title + ".xml", outStream, true); SPListItem item = formLibrary.Items[file.UniqueId]; item["TemplateUrl"] = fileInfoPath; item.Update(); forms.Update(); } catch { } } } } catch { } } Everything is ready. Let’s deploy the solution in our site and enjoy the results.        Downloading the file demo : Appvity.InfoPathFormSoln.rar (91.82 kb).

How to Create a New SharePoint Site from the Existing One

By Phong Dang posted June 4, 2013 22:24 | 2 Comments

By Phong Dang posted June 4, 2013 22:24 | 2 Comments

When you work with SharePoint, you can easily create a new site from a predefined template. One day, a customer tells you, “I want to create a new site from an existing site, which was created without a template”. If you are a developer, how do you accomplish this? There are many solutions to this problem. I choose one involving backing up and restoring in SharePoint. First, I export my customer’s site to a shared folder, and then import it back to the SharePoint site. After that, I change the name of the new site. Now I have a new site that has the same structure as the original one. To complete these steps, I use SharePoint Object Model: SPExport and SPImport. Using a simple web part, I will show you how to create a new site from an existing site. [More]

Notes: Install SharePoint Server 2013 on Windows 2012 Server error

By Lam Le posted November 30, 2012 19:01 | 0 Comments

By Lam Le posted November 30, 2012 19:01 | 0 Comments

Microsoft just released SharePoint 2013 Preview yesterday. This is a public beta for the 4th major release of the popular portal software. As we begin the review process, a quick note for those of you who received an error when installing SharePoint 2013 on Windows 2012 Server Release Candidate. If your setup process failed to set the Application and Web Server roles during the pre-requisite step of the installation program, you might be running the installation program with a domain account. [More]

Welcome!

By Lam Le posted November 14, 2012 06:00 | 0 Comments

By Lam Le posted November 14, 2012 06:00 | 0 Comments

Hello everyone and welcome to our web site. We are a small team of developers and enthusiasts in collaboration technologies. After working in SharePoint space for a long time and building one of the most successful product companies, we have decided to focus our passion in an area which we felt would prove relevant to the next decade: enterprise mobile collaboration. We hope to learn, simplify, and craft elegant solutions for mobile information workers like you. We plan to share our stories through our blog, tips, and products. [More]
  • Contact Us
  • You can reach us at:
Copyright © Appvity 2015. All Rights Reserved.