0
votes

I am programmatically creating InfoPath forms in a form library within SharePoint 2010 from data in a CSV file. It all works fine apart from the date fields. The form will refuse to open with a format error. I have tried multiple ways of formatting the date but no luck so far. Code below...

If I format 2016-10-10 then it does show in the Forms Library view but I still can not open the form. It just shows a datatype error.

        // Get the data from CSV file.
        string[,] values = LoadCsv("ImportTest.csv");

        //Calulate how many columns and rows in the dataset
        int countCols = values.GetUpperBound(1) + 1;
        int countRows = values.GetUpperBound(0) + 1;

        string rFormSite = "siteurl";
        // opens the site
        SPWeb webSite = new SPSite(rFormSite).OpenWeb();
        // gets the blank file to copy
        SPFile BLANK = webSite.Folders["EventSubmissions"].Files["Blank.xml"];

        // reads the blank file into an xml document
        MemoryStream inStream = new MemoryStream(BLANK.OpenBinary());
        XmlTextReader reader = new XmlTextReader(inStream);
        XmlDocument xdBlank = new XmlDocument();
        xdBlank.Load(reader);
        reader.Close();
        inStream.Close();

        //Get latest ID from the list
        int itemID = GetNextID(webSite, "EventSubmissions");
        if (itemID == -1) return;

        //Iterate each row of the dataset             
        for (int row = 1; row < countRows; row++)
        {

            //display current event name
            Console.WriteLine("Event name - " + values[row, 4]);
            XmlDocument xd = xdBlank;

            XmlElement root = xd.DocumentElement;

            //Cycling through all columns of the document//   
            for (int col = 0; col < countCols; col++)
            {
                string field = values[0, col];
                string value = values[row, col];

                switch (field)
                {
                    case "startDate":
                        value = //How do format the date here ;
                        break;
                    case "endDate":
                        value = "";
                        break;
                    case "AutoFormID":
                        value = itemID.ToString();
                        break;                       
                }

                XmlNodeList nodes = xd.GetElementsByTagName("my:" + field);
                foreach (XmlNode node in nodes)
                {
                    node.InnerText = value;
                }

            }

            // saves the XML Document back as a file
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            SPFile newFile = webSite.Folders["EventSubmissions"].Files.Add(itemID.ToString() + ".xml", (encoding.GetBytes(xd.OuterXml)), true);
            itemID++;
        }

       Console.WriteLine("Complete");
        Console.ReadLine();

Thanks

1

1 Answers

0
votes

For me this worked

DateTime.Now.ToString("yyyy-MM-dd")