Thursday, August 18, 2011

Change the InfoPath Field Value using Console Application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.InfoPath.Server;
using Microsoft.Office;
using System.Data;
using Microsoft.Office.Interop.InfoPath.Xml;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.InfoPath;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           
            Application myApp = new Microsoft.Office.Interop.InfoPath.Application();
            XDocumentsCollection myXDocs = myApp.XDocuments;
            XDocument myXDoc = myXDocs.Open("C:\\Test\\Form1.xml",
               (int)XdDocumentVersionMode.xdFailOnVersionOlder);

            IXMLDOMDocument2 myXMLDoc = myXDoc.DOM as IXMLDOMDocument2;

            myXMLDoc.setProperty("SelectionNamespaces",
                "xmlns:my='http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-08-18T14:11:04'");

            IXMLDOMNodeList myNames =
               myXMLDoc.selectNodes(
               "//my:customerName[. = 'Company A']");

            if (myNames.length < 1)
                Console.WriteLine(
                   "No elements containing 'Company A' were found.");

          
            IXMLDOMNode myName = myNames.nextNode();

            while (myName != null)
            {
                myName.text = "Company B";
                myName = myNames.nextNode();

            }

            myXDoc.SaveAs("C:\\Test\\Form2.xml");
            myXDocs.Close(0);
            myApp.Quit(false);
            Console.WriteLine("Finished!");
        }
    }
}

Reference Link: Automate Changing the Value of a Field

3 comments:

Featured Post

Create SharePoint Folder Structure in Destination (Only If Not Exists)

Why This Script Is Safe You can run it multiple times It will not create duplicate folders It will only create missing folders S...

Popular posts