2016-09-16 70 views
0

我以編程方式從CSV文件中的數據在SharePoint 2010中的表單庫中創建InfoPath表單。除日期字段外,它一切正常。表單將拒絕打開格式錯誤。我已經嘗試過多種格式化日期的方法,但迄今爲止沒有運氣。下面的代碼...以編程方式在SharePoint表單庫中使用日期編制Infoopath表單

如果我格式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(); 

感謝

回答

0

對我來說,這個工作

DateTime.Now.ToString( 「YYYY-MM-DD」)

相關問題