SharePoint Offline form submission



Recently I came came across a following requirement

1) Site owner should e-mail an input fill form to an external user who is not in the network.

2) User fills the form and send it back to site owner

3) Site owner should be able to upload the form to the form library

4) All the metadata from the input form should be uploaded automatically

5) Ability for the site owner to edit the form using info-path

Approach:

1) Create an input form template using MS Word

2) Map custom infopath schema using Word Content Control Tool Kit

3) Create a webpart with upload control

4) Use OpenXML libraries to read data from docx file

1) Create an input form template using MS Word:

Enable developer tools in Word  -> Options -> Customize Ribbon -> Developer

2013-08-17 14_14_02-Word Options

Design an input from using Content controls. A quick way to do this is to design the form layout using Infopath without any input controls, copy the layout in to word and add the content controls from developer pane

2013_08_17_14_18_02_NewHealthCareProviderForm_Physician_Word

2) Map custom infopath schema using Word Content Control Tool Kit:

Since the metadata would be uploaded to form library and to provide infopath editable features, infopath schema is used here. Add all the fields to this schema.

 

<?xml version=”1.0″ encoding=”utf-8″?>
<?mso-application progid=”InfoPath.Document” versionProgid=”InfoPath.Document.3″?>
<my:myFields xmlns:xsi= ”http://www.w3.org/2001/XMLSchema-instance” xmlns:my=”http://schemas.microsoft.com/office/infopath/2003/myXSD/2013"
xmlns:xd=”http://schemas.microsoft.com/office/infopath/2003" xml:lang=”en-US”>
<my:RecipientType></my:RecipientType>
<my:FileName></my:FileName>
<my:TeachingHospital><my:TeachingHospitalName>
</my:TeachingHospitalName><my:TeachingHospitalTIN>
</my:TeachingHospitalTIN><my:TeachingHospitalNPI>
</my:TeachingHospitalNPI>
</my:TeachingHospital>
<my:PhysicianInfo>
<my:PhysicianFirstName></my:PhysicianFirstName>
<my:PhysicianMiddleName></my:PhysicianMiddleName>
</my:PhysicianInfo>
</my:myFields>

Now use codeplex solution “word content control tool kit” http://dbe.codeplex.com/ to map the schema to the word template. Open the docx file using this tool, add the xml schema and map the input fields.

2013-08-17 14_15_53-NewHealthCareProviderForm_Physician.docx - Word 2007 Content Control Toolkit

3) Create a webpart with up:oad control

Now design a visual webpart with fileupload control

HTML

<asp:FileUpload ID="docxUpload" runat="server" /> </br></br>
<asp:Button ID="submitButton" runat="server" Text="Submit" />
<p><asp:Label ID="lblError" runat="server"></asp:Label></p>

Create Child controls:

protected override void CreateChildControls()
{
userControl = (UserControl)this.Page.LoadControl(ASCXPATH);
docxUpload = (System.Web.UI.WebControls.FileUpload)this.userControl.FindControl("docxUpload");
bSubmit = (System.Web.UI.WebControls.Button)this.userControl.FindControl("submitButton");
lblError = (System.Web.UI.WebControls.Label)this.userControl.FindControl("lblError");
bSubmit.Click += new EventHandler(submitButton_Click);
Controls.Add(userControl);
base.CreateChildControls();
}

 

4) Use OpenXML libraries to read data from docx file:

On clicking the submit button, use openxml libraries (using DocumentFormat.OpenXml.Packaging) to read the custom xml and upload to the infopath form library

protected void submitButton_Click(object sender, EventArgs e)
 {

lblError.Text = "";
 try
 {

#region MyRegion
 if (docxUpload.HasFile)
 {

if (Path.GetExtension(docxUpload.FileName) == ".docx")
 {

 using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(docxUpload.PostedFile.InputStream,false))
 {
 HashSet<OpenXmlPart> partList = new HashSet<OpenXmlPart>();
 MainDocumentPart mainPart = wdDoc.MainDocumentPart;
 foreach (CustomXmlPart part in mainPart.CustomXmlParts)
 {

if (part.Uri.ToString() == "/customXml/item1.xml")
 {

XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.Load(part.GetStream());
 XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.CreateNavigator().NameTable);
 ns.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2013-07-05T15:25:11");
 XmlNode recepientType = xmlDoc.SelectSingleNode("/my:myFields/my:RecipientType", ns);
 if (recepientType.InnerText == "1")
 {
 fileName = xmlDoc.SelectSingleNode("/my:myFields/my:PhysicianInfo/my:PhysicianLastName", ns).InnerText + "_" + xmlDoc.SelectSingleNode("/my:myFields/my:PhysicianInfo/my:PhysicianFirstName", ns).InnerText + "_" + DateTime.Today.ToString("yyyy-MM-dd") + ".xml";
 }

else
 {
 fileName = xmlDoc.SelectSingleNode("/my:myFields/my:TeachingHospital/my:TeachingHospitalName", ns).InnerText + "_" + DateTime.Today.ToString("yyyy-MM-dd") + ".xml";
 }

using (SPSite site = new SPSite(SPContext.Current.Site.ID))
 {
 using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
 {

web.AllowUnsafeUpdates = true;
 SPFolder frmFolder = web.GetFolder(docLib);
 SPFile spfile = frmFolder.Files.Add(fileName, part.GetStream());
 spfile.Update();
 web.AllowUnsafeUpdates = false;

}

}

}

}

}

}
 else
 {
 lblError.Text = "please upload .docx extensions";
 }

}
 else
 {
 lblError.Text = "please upload .docx extensions";
 }
 }

 

Add this webpart to the /Forms/AllItems.aspx in the form library. Now submit the word file from this webpart.

2013_08_18_18_14_38_New_Health_Care_Provider_All_Documents