0

I have infopath form library. I have excel file which has some data. I want to create item in infopath form reading the excel values using CSOM.

1 Answer 1

0

You may use the below code:

public static void ChangeFields()
{
    //Open SharePoint site
    using (SPSite site = new SPSite("http://<SharePoint_Site_URL>"))
    {
        using (SPWeb web = site.OpenWeb())
        {
            //Get handle for forms library
            SPList formsLib = web.Lists["FormsLib"];

            if (formsLib != null)
            {
                foreach (SPListItem item in formsLib.Items)
                {
                    XmlDocument xml = new XmlDocument();

                    //Open XML file and load it into XML document
                    using (Stream s = item.File.OpenBinaryStream())
                    {
                        xml.Load(s);
                    }

                    //Do your stuff with xml here. This is just an example of setting a boolean field to false.
                    XmlNodeList nodes = xml.GetElementsByTagName("my:SomeBooleanField");
                    foreach (XmlNode node in nodes)
                    {
                        node.InnerText = "0";
                    }

                    //Get binary data for new XML
                    byte[] xmlData = System.Text.Encoding.UTF8.GetBytes(xml.OuterXml);

                    using (MemoryStream ms = new MemoryStream(xmlData))
                    {
                        //Write data to SharePoint XML file
                        item.File.SaveBinary(ms);
                    }
                }
            }
        }
    }
}

Source:

Programmatically Edit Infopath Form Fields?

Read Excel file using C# coding:

private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            Excel.Range range ;

            string str;
            int rCnt ;
            int cCnt ;
            int rw = 0;
            int cl = 0;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(@"d:\csharp-Excel.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            range = xlWorkSheet.UsedRange;
            rw = range.Rows.Count;
            cl = range.Columns.Count;


            for (rCnt = 1; rCnt  < = rw; rCnt++)
            {
                for (cCnt = 1; cCnt  < = cl; cCnt++)
                {
                    str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                    MessageBox.Show(str);
                }
            }

            xlWorkBook.Close(true, null, null);
            xlApp.Quit();

            Marshal.ReleaseComObject(xlWorkSheet);
            Marshal.ReleaseComObject(xlWorkBook);
            Marshal.ReleaseComObject(xlApp);

        }

Source:

How to read an Excel file using C#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.