[英]Programmatically create Infopath form in SharePoint form library with dates
I am programmatically creating InfoPath forms in a form library within SharePoint 2010 from data in a CSV file. 我正在以编程方式从CSV文件中的数据在SharePoint 2010中的表单库中创建InfoPath表单。 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. 如果我格式化2016-10-10,那么它的确会显示在“表单库”视图中,但是我仍然无法打开表单。 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 谢谢
For me this worked 对我来说这很有效
DateTime.Now.ToString("yyyy-MM-dd") DateTime.Now.ToString(“ yyyy-MM-dd”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.