Friday, 9 November 2012

Data Migration Excel to Infopath 2010 form in sharepoint 2010 Form Library

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;
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;


namespace Example
{
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();
            }
        }
       
    }
}




Monday, 8 October 2012

Infopath 2010 Drop-Down List Box (Get choices from fields in this form)

Binding Dropdown in infopath form load without using Data Connections ( Dropdown [Get choices from fields in this form]) using code behind.

  • Go to Infopath form, Add one Group (like test),set properties -check (Repeating).
  • Add two fields (like test_displayname and test_value) under Group.
  • Add one Dropdown,set properties (Get choices from fields in this form).
  • set the value as Group [like test_displayname and test_value].
  • go to on form load events write the code.
public void FormEvents_Loading(object sender, LoadingEventArgs e)
        {
            XPathNavigator myRootNode = MainDataSource.CreateNavigator();
            XPathNavigator userNode = myRootNode.SelectSingleNode("/my:myFields/my:test", NamespaceManager);
            using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList userlist = web.Lists["Departments"];
                     SPListItemCollection usercollection = userlist.GetItems();
                    if (usercollection.Count > 0)
                    {
                        foreach (SPListItem item in usercollection)
                        {
                            XPathNavigator newNode = null;
                            newNode = userNode.Clone();
                            newNode.SelectSingleNode("/my:myFields/my:test/my:test_displayname", this.NamespaceManager).SetValue(item.Title);
                            newNode.SelectSingleNode("/my:myFields/my:test/my:test_value", this.NamespaceManager).SetValue(item.Title);
                            userNode.InsertAfter(newNode);
                            newNode = null;
                        }
                        userNode.DeleteSelf();
                        userNode = null;
                    }
                }
            }

How to submit data from an InfoPath 2010 form to SharePoint 2010 List, using web service.

Please see the below url :

It's help to make insert item in sharepoint list using web services.

http://geekswithblogs.net/KunaalKapoor/archive/2012/04/30/how-to-submit-data-from-an-infopath-2010-form-to.aspx

Write html (like Hyper Link) in RichText Box in Infopath 2010 for Sharepoint list data.

1. Take (field1) one Richtext box and  (txturl) one text box fields.
2. On Form Load, Bind data connection toTextBox Controls.
3. Go to Infopath 2010 Form load event, write the code.

public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
XPathNavigator attachmentNav = MainDataSource.CreateNavigator();
string url = attachmentNav.SelectSingleNode("/my:myFields/my:txturl", NamespaceManager).Value;
attachmentNav.SelectSingleNode("/my:myFields/my:field1", NamespaceManager).InnerXml = "<p xmlns='http://www.w3.org/1999/xhtml'>" + url + "</p>";
}

In ulr=" Welcome to the sharepoint 2010 with  <a  href='http:/www.google.com'>Infopath 2010</a>

Example: Welcome to the sharepoint 2010 with Infopath 2010


Make Document Set using infopath code in sharepoint 2010.

1.First create one Document Library and go to library sittings.
2.Open Advanced setting, then click "Yes" Allow management of centent types.
3.then Add from existing site content types , select Document Set.
4.Go to Infopath 2010, add one check box write code one change event in check box.

            XPathNavigator myRootNode = MainDataSource.CreateNavigator();
             myRootNode.SelectSingleNode(" /my:myFields/my:field4", NamespaceManager).SetValue("rintu");
            try
            {
               
                SPWeb web = SPContext.Current.Web;
                myRootNode.SelectSingleNode(" /my:myFields/my:field4", NamespaceManager).SetValue(web.Url.ToString());
                SPList list = web.Lists["BriefingDoc"];
                string sguid = System.Guid.NewGuid().ToString();
                web.AllowUnsafeUpdates = true;
                string sDocsetName = string.Empty;
                sDocsetName = "Document Set";
                DocumentSet ds = DocumentSet.Create(list.RootFolder, sguid, list.ContentTypes[sDocsetName].Id, null, true);
                web.AllowUnsafeUpdates = false;
                string DocUrl = ds.WelcomePageUrl.Replace("Document Set", "Document%20Set");
                myRootNode.SelectSingleNode("/my:myFields/my:field2/@my:field3", NamespaceManager).SetValue(DocUrl);

            }
            catch (Exception ex)
            {
                myRootNode.SelectSingleNode("/my:myFields/my:DatadeTérminoMessage", NamespaceManager).SetValue(ex.Message);
            }
            }

5.see that one hyper link is created, click that and upload the documents.

Friday, 14 September 2012

Getting Repeater Section current row in InfoPath 2010 using code.

Using following code:-

-Take one repeater section under section add one fields to see the item count or  current row value.
-Then on fire repeater section event change binding.


XPathNavigator root = this.CreateNavigator();
XPathNodeIterator nodes = 
   root.Select("/my:myFields/my:group1/my:field1", NamespaceManager);
int myInt = 1;
while (nodes.MoveNext())
{
   nodes.Current.SetValue(myInt.ToString());
   myInt = myInt + 1;
}

Friday, 7 September 2012

Data Binding with SharePoint 2010 list in InfoPath 2010

Please see the code:

public void AddCountries()
        {
            try
            {
                SPSite site = new SPSite("http://win-67038mbkel7");
                SPWeb web = site.OpenWeb();
                SPList list = web.Lists["Countries"];
                SPListItemCollection listitems = list.Items;
                XPathNavigator nav = this.CreateNavigator().
                    SelectSingleNode("/my:myFields/my:Countries", this.NamespaceManager);// Note: "/my:myFields/my:Countries" => Group Countries
                foreach (SPListItem li in listitems)
                {
                    XPathNavigator newNode = null;
                    newNode = nav.Clone();
                    newNode.SelectSingleNode("/my:myFields/my:Countries/my:Displayname",
                        this.NamespaceManager).SetValue(li["Title"].ToString());
                    newNode.SelectSingleNode("/my:myFields/my:Countries/my:Value",
                        this.NamespaceManager).SetValue(li["Title"].ToString());
                    nav.InsertAfter(newNode);
                    newNode = null;
                }
                nav.DeleteSelf();
                nav = null;
            }
            catch
            {
            }
        }