Monday, September 27, 2010

Updating the data from XML to Sharepoint List

With the use of Sharepoint List Webservice i am going to populate the xml data into the list. for this you need to create a list and also the columns. with nodes in the XML.

In this i m a  going to hard the Webservice Url, but you can add the app.config and can adda new key and value . which it reference in the code using Cofiguration Settings.by inheriting the System.Configuration.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Net;
using System.IO;

namespace XMLtoSPList
{
    class Program
    {
        static void Main(string[] args)
        {

            try
            {
                ReferenceToWebService.Lists ListsService = new ReferenceToWebService.Lists();
                ListsService.Credentials = new NetworkCredential("XXXX", "YYYYYY", "ZZZZZZ");
                //ListsService.Credentials = System.Net.CredentialCache.DefaultCredentials;

                string _listservURL = string.Empty;
                Console.WriteLine("Enter the SiteCollection Url :", _listservURL);
                _listservURL = Console.ReadLine();
                ListsService.Url = "http://servername/_vti_bin/lists.asmx";

                //ListsService.Url = "http://myserver:6580/Docs/_vti_bin/lists.asmx";
                XmlTextReader textReader = new XmlTextReader("C:\\myxml.XML");
                textReader.Read();



                XmlDocument doc = new XmlDocument();


                XmlElement batch = doc.CreateElement("Batch");

                batch.SetAttribute("OnError", "Continue");
                batch.SetAttribute("ListVersion", "1");


                XmlDocument doc1 = new XmlDocument();
                doc1.Load("C:\\Duplicate Vendor Additions.XML");
                int i = 1;
                try
                {
                    ListsService.GetList("RECORDS");
                    System.Xml.XmlNode ndListView = ListsService.GetListAndView("RECORDS", "");
                    XmlNode node1 = ndListView.ChildNodes[1];
                    XmlNode node2 = node1.ChildNodes[1];
                    XmlNodeList node3 = node2.ChildNodes;
                    int count = 0;
                  
                  
                    string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
                    foreach (XmlNode nd in doc1.DocumentElement.SelectNodes("RECORD"))
                    {
                        try
                        {
                            string Duplicates = nd.ChildNodes[0].InnerText;
                            batch.InnerXml = "" +
                                "" + i.ToString() + "" +
                                "" + nd.ChildNodes[0].InnerText.Trim() + "" +
                                   "" + nd.ChildNodes[1].InnerText.Trim() + "" +
                                     "" + nd.ChildNodes[2].InnerText.Trim() + "" +
                                       "" + nd.ChildNodes[3].InnerText.Trim() + "" +
                                         "" + nd.ChildNodes[4].InnerText.Trim() + "" +
                                 "" + nd.ChildNodes[5].InnerText.Trim() + "" +
                                 "" + nd.ChildNodes[6].InnerText.Trim() + "" +
                                 "";
                        }
                        catch (Exception ex)
                        {
                            Trace(DateTime.Now.ToString() + " A record with " );
                            Trace(nd.InnerXml);
                            Trace("is not inserted.");
                        }
                        //ListsService.UpdateListItems(strListID, batch);


                        Console.WriteLine(nd.ChildNodes[0].InnerText);
                        i++;
                    }
                }
                catch(Exception ex)
                {
                   Trace(DateTime.Now.ToString()+"---No List Found.");
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            Console.WriteLine("Done");
            string exit = string.Empty;

            Console.WriteLine("Press X to exit"+exit);
            if (exit == "x")
            {
                exit = "exit";
                exit = Console.ReadLine();
            }
            Console.Read();


        }
    }
}

No comments:

Post a Comment