How to data migration from excel to infopath 2010 form in sharepoint 2010 form library?
Following are the important file:
First make xml file: sample.xml<?xml version="1.0"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:example:-myXSD-2012-03-23T05-13-59" solutionVersion="1.0.0.903" productVersion="14.0.0.0" PIVersion="1.0.0.0" href="https://server-name/sites/site-name/FormServerTemplates/example.xsn"?><?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-03-23T05:13:59" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US">
<my:GeneralInfo>
<my:ddDivision>NameExample_mapped</my:ddDivision>
</my:GeneralInfo>
Second make excel file: sources.xlsx
one column name-NameExample
Code for Data Migrations:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using System.IO;
using System.Xml;
using System.Data.OleDb;
using System.Data;
using System.Collections;
using System.Globalization;
using System.Threading;
using System.Net;
{
class Program
{
static string strURL = "https://app-int.eu.novartis.net/ph/exmaple/";
static string strFormsFolder = @"D:\datamir\exmaple\Forms\";
static string strSampleForm = @"D:\datamir\exmaple\Sample.xml";
static NetworkCredential credentials = new NetworkCredential("test", "test", "server");
static DataSet dataSet = null;
static StringBuilder sbSampleForm = null;
static void Main(string[] args)
{
try
{
CreateInfoPathForms();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}private static void CreateInfoPathForms()
{
ReadXLS();
ReadSampleForm();
ArrayList arFilename = new ArrayList();
foreach (DataRow dr in dataSet.Tables[0].Rows)
{
try
{
string strContract = sbSampleForm.ToString();
foreach (DataColumn dc in dataSet.Tables[0].Columns)
{
strContract = strContract.Replace(dc.ColumnName.ToString() + "_mapped", System.Security.SecurityElement.Escape(dr[dc.ColumnName].ToString()));
}
string storeDatetime;
try
{
storeDatetime = DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH'.'mm'.'ss.fffffff");
string strFileName = dr["NameExample"].ToString() + "-" + storeDatetime + ".xml";
if (!arFilename.Contains(strFileName))
{
arFilename.Add(strFileName);
}
else
{
storeDatetime = DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH'.'mm'.'ss.fffffff") + intCount.ToString();
strFileName = dr["GEMCountry"].ToString() + "-" + DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH'.'mm'.'ss.fffffff") + storeDatetime + ".xml";
arFilename.Add(strFileName);
}
CreateFormFromTemplate(strFileName, strContract);
Console.WriteLine("Form is created");
ServicePointManager.ServerCertificateValidationCallback = (obj, certificate, chain, errors) => true;
ClientContext context = new ClientContext(strURL);
context.Credentials = credentials;
Web web = context.Web;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(strFormsFolder + strFileName);
newFile.Url = strFileName;
List docs = web.Lists.GetByTitle("Lib");
Microsoft.SharePoint.Client.Folder uploadFile = docs.RootFolder.Folders.Add(dr["Country"].ToString());
Microsoft.SharePoint.Client.File fileup = uploadFile.Files.Add(newFile);
context.Load(fileup);
context.ExecuteQuery();
WriteLog("Uploaded to SharePoint site is completed.");
Console.WriteLine("Form is created");
}
catch (Exception ex1)
{
}
}
}
catch(Exception ex)
{
}
}
}
private static void ReadSampleForm()
{
StreamReader sr = null;
sbSampleForm = new StringBuilder();
try
{
sr = new StreamReader(strSampleForm);
sbSampleForm.Append(sr.ReadToEnd());
}
catch (Exception ex)
{
WriteLog("Failed");
WriteLog(ex.StackTrace);
}
finally
{
sr.Close();
}
}
private static void CreateFormFromTemplate(string strFormName, string strFormDetails)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(strFormsFolder+strFormName);
sw.WriteLine(strFormDetails);
}
catch (Exception ex)
{
WriteLog("Failed");
WriteLog(ex.StackTrace);
}
finally
{
sw.Flush();
sw.Close();
}
}
}
}
No comments:
Post a Comment