Monday, 14 May 2012

Infopath submit to list

Following from http://msdn.microsoft.com/en-us/library/cc162745(v=office.12).aspx

Project and EmailType list will be populated with entries from InfoPath.

So each list will require data connections of

  1. Template in xml format for retrieving data
  2. Retriving data from the SharePoint list
  3. Submit data type to the _vti_bin/lists.asmx service of the SharePoint site

Below I have for Project, AddProjectTemplate(1), TestProject(2), Web Service Submit Proj (3).
Similiarly for EmailSequence, AddEmailSequenceTemplate(1), TestEmailSequence(2), Web Service Submit(3)



FormCode.cs
using Microsoft.Office.InfoPath;
using System;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using mshtml;

namespace MyEmailType
{
    public partial class FormCode
    {
        public void InternalStartup()
        {
            EventManager.XmlEvents["/my:myFields/my:AddEmailType"].Changed += new XmlChangedEventHandler(AddEmailType_Changed);
            ((ButtonEvent)EventManager.ControlEvents["btnSubmit"]).Clicked += new ClickedEventHandler(btnSubmit_Clicked);             
            ((ButtonEvent)EventManager.ControlEvents["btnCancel"]).Clicked += new ClickedEventHandler(btnCancel_Clicked);
        }

        public void AddEmailType_Changed(object sender, XmlEventArgs e)
        {
            try
            {
                // Get a reference to the form's XmlNamespaceManager object.
                XmlNamespaceManager ns = this.NamespaceManager;

                // Create an XPathNavigator object for the form's main data
                // source.
                XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

                // If the check box is set to true, add a new row so the user 
                // can add a new contact.
                if (e.Site.Value == "true")
                {
                    XPathNavigator xnTable = xnDoc.SelectSingleNode("/my:myFields/my:gpEmailTypes", ns);
                    xnTable.AppendChild("");
                }
                else
                {
                    // If the user clears the check box, remove the added row.
                    ClearEnteredValues(xnDoc);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("AddEmailType_Changed error occurred: " + ex.Message);
                throw;
            }
        }
        private void ClearEnteredValues(XPathNavigator xnDoc)
        {
            try
            {
                // Get a reference to the form's XmlNamespaceManager object.
                XmlNamespaceManager ns = this.NamespaceManager;

                // Create an XPathNodeIterator object to get a count of the 
                // rows in the repeating table used to add new Contacts.
                XPathNodeIterator xi = xnDoc.Select(
                   "/my:myFields/my:gpEmailTypes/my:gpEmailType", ns);
                int rowCount = xi.Count;

                if (rowCount > 0)
                {
                    // Get the first and last rows (nodes) in the 
                    // repeating table.
                    XPathNavigator firstNode = xnDoc.SelectSingleNode(
                       "/my:myFields/my:gpEmailTypes/my:gpEmailType[1]", ns);
                    XPathNavigator lastNode = xnDoc.SelectSingleNode(
                       "/my:myFields/my:gpEmailTypes/my:gpEmailTypes[" +
                       rowCount + "]", ns);

                    // Delete the existing nodes using the DeleteRange method.
                    firstNode.DeleteRange(lastNode);
                }

                // Clear the check box. 
                xnDoc.SelectSingleNode(
                   "/my:myFields/my:AddEmailType", ns).SetValue("false");
            }

            catch (Exception ex)
            {
                MessageBox.Show("The following error occurred: " +
                   ex.Message);
                throw;
            }
        }

        public void btnSubmit_Clicked(object sender, ClickedEventArgs e)
        {
            try
            {                 
                XmlNamespaceManager ns = this.NamespaceManager;
 
                DataSource dsEmailSequence = this.DataSources["TestEmailSequence"];
 
                XPathNavigator xnEmailSequence = dsEmailSequence.CreateNavigator();

                XPathNodeIterator xiEmailSequence = xnEmailSequence.Select("/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW", ns);
 
                DataSource dsCAML = this.DataSources["AddEmailSequenceTemplate"];
 
                XPathNavigator xnCAML = dsCAML.CreateNavigator();
 
                WebServiceConnection wsSubmit = (WebServiceConnection)this.DataConnections["Web Service Submit"];
 
                XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

                XPathNodeIterator xiNewEmailType = xnDoc.Select("/my:myFields/my:gpEmailTypes/my:gpEmailType", ns);
                string projectTitle = xnDoc.SelectSingleNode("//my:myFields/my:ProjectTitle", NamespaceManager).Value;
                if (xiNewEmailType.Count > 0)
                {
                    while (xiNewEmailType.MoveNext())
                    {
                        
                        xnCAML.SelectSingleNode("/Batch/Method/Field[@Name='Title']", ns).SetValue(projectTitle);
                        xnCAML.SelectSingleNode("/Batch/Method/Field[@Name='EmailType']", ns).SetValue(xiNewEmailType.Current.SelectSingleNode("my:EmailType",ns).Value);
                        xnCAML.SelectSingleNode("/Batch/Method/Field[@Name='SeqFrom']", ns).SetValue(xiNewEmailType.Current.SelectSingleNode("my:SeqFrom", ns).Value);
                        xnCAML.SelectSingleNode("/Batch/Method/Field[@Name='SeqTo']", ns).SetValue(xiNewEmailType.Current.SelectSingleNode("my:SeqTo", ns).Value);
                        
                        xnCAML.SelectSingleNode("/Batch/Method/@Cmd", ns).SetValue("New");
                        // Submit the new row.
                        if(xiNewEmailType.Current.SelectSingleNode("my:SeqTo", ns).Value.Length>0)
                            wsSubmit.Execute();
                    }
                }
                
                // submit form add to project's info Project list
                WebServiceConnection wsSubmitProject = (WebServiceConnection)this.DataConnections["Web Service Submit Proj"];
                DataSource dsCAMLproject = this.DataSources["AddProjectTemplate"];

                XPathNavigator xnCAMLproject = dsCAMLproject.CreateNavigator();

                xnCAMLproject.SelectSingleNode("/Batch/Method/Field[@Name='Title']", ns).SetValue(projectTitle);
                wsSubmitProject.Execute();
                this.Application.ActiveWindow.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show("The following error occurred: " + ex.Message);
                Console.WriteLine(ex.Message);
                throw;
            }
        }

        public void btnCancel_Clicked(object sender, ClickedEventArgs e)
        {
            this.Application.ActiveWindow.Close();
        }
    }
}

AddEmailSequenceTemplate.xml

<Batch OnError="Continue">
    <Method ID="1" Cmd="New">
        <Field Name='Title'></Field>
        <Field Name="EmailType"></Field>
        <Field Name="SeqFrom"></Field>
        <Field Name="SeqTo"></Field> 
        <Field Name="ID"></Field>
    </Method>
</Batch>

AddProjectTemplate.xml is similiar to above except with only Title and ID

No comments:

Post a Comment