Thursday, 6 September 2012

Add List Item Using Infopath Form SharePoint 2010


using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using Microsoft.SharePoint;

namespace Form1
{
    public partial class FormCode
    {
        // Member variables are not supported in browser-enabled forms.
        // Instead, write and read these values from the FormState
        // dictionary using code such as the following:
        //
        // private object _memberVariable
        // {
        //     get
        //     {
        //         return FormState["_memberVariable"];
        //     }
        //     set
        //     {
        //         FormState["_memberVariable"] = value;
        //     }
        // }

        // NOTE: The following procedure is required by Microsoft InfoPath.
        // It can be modified using Microsoft InfoPath.
        public void InternalStartup()
        {
            ((ButtonEvent)EventManager.ControlEvents["CTRL5_5"]).Clicked += new ClickedEventHandler(CTRL5_5_Clicked);
        }

        public void CTRL5_5_Clicked(object sender, ClickedEventArgs e)
        {
            SPSite site = new SPSite("siteurl");
            SPWeb web = site.OpenWeb();
            web.AllowUnsafeUpdates = true;
            SPList InfopathList = web.Lists["InfopathList"];
         
            XPathNavigator xnav = MainDataSource.CreateNavigator();
            XPathNodeIterator rows = xnav.Select("/my:myFields", NamespaceManager);
            while (rows.MoveNext())
            {
                SPListItem item = InfopathList.Items.Add();

                string Title = rows.Current.SelectSingleNode("@my:Title", NamespaceManager).Value;
                string EmployeeId = rows.Current.SelectSingleNode("my:EmployeeId", NamespaceManager).Value;
                string EmployeeName = rows.Current.SelectSingleNode("my:EmployeeName", NamespaceManager).Value;


                item["Title"] = Title;
                item["EmployeeId"] = EmployeeId;
                item["EmployeeName"] = EmployeeName;
                item.Update();
            }
            XPathNavigator x1 = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/@my:Title", this.NamespaceManager);
            XPathNavigator x2 = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:EmployeeId", this.NamespaceManager);
            XPathNavigator x3 = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:EmployeeName", this.NamespaceManager);

            x1.SetValue("");
            x2.SetValue("");
            x3.SetValue("");
        }
    }
}

1 comment: