Monday, February 01, 2010

How to update Infopath form fields Programatically?

public static void UpdateInfopathFormData(SPFile File, string NodeXPath, string UpdateValue,string NameSpace)
{
XmlDocument Document = new XmlDocument();
Document.Load(File.OpenBinaryStream()); // Get the FDRY document file into an XML Document

XPathNavigator root = Document.CreateNavigator();
XmlNamespaceManager nsm = new XmlNamespaceManager(Document.NameTable);
nsm.AddNamespace("my", NameSpace);//Get the NameSpace from infopath form(Open Infopath form in design mode. Click on datasource from design tasks.Click on any field. Go to properties. Go to Detalis Tab. Copy the "Namespace" value).

SetValueSpecial(root, NodeXPath, UpdateValue, nsm);
Byte[] data = Encoding.UTF8.GetBytes(Document.OuterXml);
File.SaveBinary(data);
}

public static void SetValueSpecial(XPathNavigator Root, string path, string value, XmlNamespaceManager NamespaceManager)
{

XPathNavigator node = Root.SelectSingleNode(path, NamespaceManager);

if (node.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))
{
node.DeleteSelf();
}
node.SetValue(value);
}

No comments: