Tuesday, October 25, 2011

Update InfoPath 2010 form Programmatically

Recently worked on a requiement on InfoPath 2010 custom form.

Expectations:
  1. User already submitted customized InfoPath form in from library.
  2. Using Upload Control, user have uploaded the documents on Form and not submitted these forms in document library.
  3. We need to generate a URL on existing textbox after getting the document name from this attachment control and concatinte this document name wiht the document library URL for all 400 forms.


Implementation Approach:
I created a customized InfoPath forms and published it to each environment.

Steps:
  1. User can select the SPSite, SPWeb and Form Library from the dropdown.
  2. Search for all the forms in the library which needs to be fix.
  3. Load these form in a multi select box as a datasource and user can select from the UI to fix the form.


Reference DLLs:
using Microsoft.Office.InfoPath;

using System;
using System.Xml;
using System.Xml.XPath;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Web;

Also reference: To handle the attached file operations.
InfoPathAttachmentDecoder.cs
InfoPathAttachmentEncoder.cs
*******************
using (SPSite site = new SPSite("Your Site Name"))
{
using (SPWeb web = site.OpenWeb())
{
SPList formsLib = web.Lists["Your Form Library Name"];

// TIP: Loading a generic list.

// This loaded as a datasource for the multiselect dropdown.
List mulSelectDropDownValue = new List();

// TIP: XPath Navigator.

XPathNavigator domNav = MainDataSource.CreateNavigator();

if (formsLib != null)
{
foreach (SPListItem item in formsLib.Items)
{
SPFile ipForm = item.File;

string fileName = ipForm.Name;

byte[] binFile = ipForm.OpenBinary();

Stream strm = new MemoryStream(binFile);

string fileNameParam = string.Empty;

// TIP: Loading a XML Document to hold the file

XmlDocument xd = new XmlDocument();

xd.Load(strm);

string textValuePlanning = string.Empty;

string textValueAccounting = string.Empty;



// TIP: Searching the node name in the XML tree, in my case it’s . “my:DoucmentName_1”

XmlNodeList _listDocName = xd.GetElementsByTagName("my:DoucmentName_1");

foreach (XmlNode _listDoc in _listDocName)

{

if (_listDoc.InnerText != "")
{

// TIP: InfoPathAttachment Decoder Class to get the attached file on attachment control in my case.

InfoPathAttachmentDecoder dec = new InfoPathAttachmentDecoder(_listDoc.InnerText.ToString());

string _fileName = dec.Filename;

if (_fileName != "")

{

// TIP: Actual first node name to write value

XmlNodeList _listAcc = xd.GetElementsByTagName("my:accounting_doc_location");

XmlNodeList _linkAccounting = xd.GetElementsByTagName("my:clicklink");

foreach (XmlNode linkNode in _linkAccounting)
{
linkNode.InnerText = "Click here to view the file";}
foreach (XmlNode node in _listAcc)
{

textValueAccounting = node.InnerText.ToString();

if (textValueAccounting.ToString() == "")
{node.InnerText = “Put the value to overwrite…”
}}}} }}

Note: This is not the complete code as implemented, for complete code please mail me on suneet_sharma@yahoo.com referring this post.

Cheers!!!
Suneet Sharma

No comments: