There is a list with name: NibQuizQuestionsOld
Sharepoint form name: TestNibQuizQuestion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Net;
using Microsoft.SharePoint.Client;
namespace CreateInfopathFormConsole
{
class Program
{
static void Main(string[] args)
{
byte[] infoPathFormData = null;
#region Read List data - Start
string siteUrl = "http://site.com/teams/site_Cust";
ClientContext clientContext = new ClientContext(siteUrl);
// before deploying to another site - change ListName
//change sitename, change formlibrary name, change the WriteProcessingInstruction - template .xsn
List categoryList = clientContext.Web.Lists.GetByTitle("NibQuizQuestionsOld");
CamlQuery query = new CamlQuery();
query.ViewXml =
"
";
ListItemCollection quizItemCollection = categoryList.GetItems(query);
clientContext.Load(quizItemCollection);
clientContext.ExecuteQuery();
ListItem[] quizQuestionItemArray = quizItemCollection.ToArray();
# endregion
#region - Create Inforpath Form
for (int k = 0; k < quizQuestionItemArray.Count(); k++)
{
int name = k + 1;
using (MemoryStream ms = new MemoryStream())
{
using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
// Create the required processing instructions
writer.WriteProcessingInstruction(
"mso-infoPathSolution",
"name=\"urn:schemas-microsoft-com:office:infopath:NibQuizQuestion:-myXSD-2013-05-25T15-05-01\" solutionVersion=\"1.0.0.78\" productVersion=\"14.0.0.0\" PIVersion=\"1.0.0.0\" href=\"http://site.com/teams/site_Cust/TestNibQuizQuestion/Forms/template.xsn\"");
writer.WriteProcessingInstruction(
"mso-application",
"progid=\"InfoPath.Document\" versionProgid=\"InfoPath.Document.3\"");
// Create the XML start data element: <my:myFields> with three namespaces
writer.WriteStartElement("my", "myFields", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2013-05-25T15:05:01");
writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xmlns", "xhtml", null, "http://www.w3.org/1999/xhtml");
writer.WriteAttributeString("xmlns", "pc", null, "http://schemas.microsoft.com/office/infopath/2007/PartnerControls");
writer.WriteAttributeString("xmlns", "ma", null, "http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes");
writer.WriteAttributeString("xmlns", "d", null, "http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields");
writer.WriteAttributeString("xmlns", "q", null, "http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields");
writer.WriteAttributeString("xmlns", "dfs", null, "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution");
writer.WriteAttributeString("xmlns", "dms", null, "http://schemas.microsoft.com/office/2009/documentManagement/types");
writer.WriteAttributeString("xmlns", "xd", null, "http://schemas.microsoft.com/office/infopath/2003");
writer.WriteAttributeString("xml", "lang", null, "en-US");
// Fill the date field with today's date
// writer.WriteString(DateTime.Now.ToString("yyyy-MM-dd"));
string[] arrAnswers = Convert.ToString(quizQuestionItemArray[k].FieldValues["Answer"]).Split(';');
int corrAnsNo = 0;
writer.WriteStartElement("my", "group1", null);
for (int i = 1; i <= arrAnswers.Count(); i++)
{
if (quizQuestionItemArray[k].FieldValues["CorrectAnswer"].ToString() == arrAnswers[i-1])
{
corrAnsNo = i;
}
writer.WriteStartElement("my", "group2", null);
writer.WriteStartElement("my", "Sno", null);
writer.WriteString(i.ToString());
writer.WriteEndElement(); // end of Sno
writer.WriteStartElement("my", "Answer", null);
writer.WriteString(arrAnswers[i-1]);
writer.WriteEndElement(); // end of Answer
writer.WriteEndElement(); // end of group 2
}
writer.WriteEndElement(); // end of group 1
writer.WriteStartElement("my", "CorrectAnswer", null);
writer.WriteString(corrAnsNo.ToString());
writer.WriteEndElement();
writer.WriteStartElement("my", "Status", null);
writer.WriteString(quizQuestionItemArray[k].FieldValues["Status"].ToString().ToLower());
writer.WriteEndElement();
writer.WriteStartElement("my", "Questions", null);
writer.WriteString(quizQuestionItemArray[k].FieldValues["Title"].ToString());
writer.WriteEndElement();
writer.WriteStartElement("my", "FormName", null);
writer.WriteString(name.ToString());
writer.WriteEndElement();
writer.WriteStartElement("my", "CorrectAnswers", null);
writer.WriteString(quizQuestionItemArray[k].FieldValues["CorrectAnswer"].ToString());
writer.WriteEndElement();
writer.WriteStartElement("my", "Message", null);
//writer.WriteString();
writer.WriteEndElement();
writer.WriteStartElement("my", "StoreMaxID", null);
writer.WriteString("NaN");
writer.WriteEndElement();
writer.WriteStartElement("my", "Crop", null);
writer.WriteString(quizQuestionItemArray[k].FieldValues["Crop"].ToString());
writer.WriteEndElement();
writer.WriteEndElement(); // end myfield
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
infoPathFormData = ms.ToArray();
ms.Close();
}
#endregion
// Upload the newly created InfoPath form to SharePoint
if (infoPathFormData != null && infoPathFormData.Length != 0)
{
using (WebClient client = new WebClient())
{
// Set the credentials to be used for upload to SharePoint
client.Credentials = CredentialCache.DefaultCredentials;
//http://site.com/teams/site_Cust/TestNibQuizQuestion/newform1.xml
//http://site2.com/teams/nnnn/TestNibQuizQuestion/"+name+".xml
//http://site.com/teams/site_Cust/TestNibQuizQuestion/"+name.ToString()+".xml
// Upload the newly created form to a SharePoint form library
client.UploadData(
@"http://site.com/teams/site_Cust/TestNibQuizQuestion/" + name.ToString() + ".xml",
"PUT",
infoPathFormData);
Console.WriteLine("New infopathForm Created successfully - " + name);
Console.ReadLine();
}
}
} // close for loop
}
}
}