【问题标题】:Programmatically create Infopath form in SharePoint form library with dates以编程方式在 SharePoint 表单库中创建带有日期的 Infopath 表单
【发布时间】:2017-03-23 18:07:51
【问题描述】:

我正在使用 CSV 文件中的数据以编程方式在 SharePoint 2010 的表单库中创建 InfoPath 表单。除了日期字段之外,它一切正常。表单将因格式错误而拒绝打开。我尝试了多种格式化日期的方法,但到目前为止还没有运气。下面的代码...

如果我格式化 2016-10-10,它会显示在表单库视图中,但我仍然无法打开表单。它只是显示数据类型错误。

        // 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();

谢谢

【问题讨论】:

    标签: forms sharepoint infopath


    【解决方案1】:

    对我来说这很有效

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多