Wednesday 30 January 2013

Reading data from CSV File using JavaScript



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Reading Data from CSV File</title>
        <style type="text/css">
            div
            {
                float: left;
                width: 250px;
            }
        </style>
        <script type="text/javascript">
            function readCSVFile() {
                var csvData = new Array();
                var csvFilePath = "F:\\Data.csv";

                var fso = new ActiveXObject('Scripting.FileSystemObject');
                var iStream = fso.OpenTextFile(csvFilePath, 1, true, 0);
                for (var n = 0; !iStream.AtEndOfStream; n++) {
                    csvData[n] = iStream.ReadLine();
                    showData(csvData[n]);
                }
                iStream.Close();
            }

            function showData(data) {
                var csvData = data.split(",");
                document.getElementById('firstNames').innerHTML += "<br />" + csvData[0];
                document.getElementById('middleNames').innerHTML += "<br />" + csvData[1];
                document.getElementById('lastNames').innerHTML += "<br />" + csvData[2];
            }
        </script>
    </head>
    <body onload="javascript: readCSVFile()">
        <div id="firstNames"><b><i>First Names:</i></b></div>
        <div id="middleNames"><b><i>Middle Names:</i></b></div>
        <div id="lastNames"><b><i>Last Names:</i></b></div>
    </body>
</html>

Note: This code works only in Internet Explorer

Writing Data to CSV File using JavaScript


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Writing Data to CSV</title>
        <script type="text/javascript">
            function addToCSVFile() {
                var csvData = new Array();  // To collect the names
                var csvFilePath = "F:\\Data.csv"; // File name

                // Collecting the names
                csvData[0] = document.getElementById('firstName').value;
                csvData[1] = document.getElementById('middleName').value;
                csvData[2] = document.getElementById('lastName').value;

                var fso = new ActiveXObject('Scripting.FileSystemObject');
                var oStream = fso.OpenTextFile(csvFilePath, 8, true, 0);
                oStream.WriteLine(csvData.join(','));
                oStream.Close();
                clearData();
                alert("Data Added Successfully");
            }

            function clearData() {
                document.getElementById('firstName').value = "";
                document.getElementById('middleName').value = "";
                document.getElementById('lastName').value = "";
            }
        </script>
    </head>
    <body>
        <p>
            First Name: <input type="text" id="firstName" />
            <br />
            Middle Name: <input type="text" id="middleName" />
            <br />
            Last Name: <input type="text" id="lastName" />
            <br />
            <input type="button" id="addButton" value="Add to CSV File" onClick="javascript: addToCSVFile()" />
        </p>
    </body>
</html>

Note: This code only works in Internet Explorer.



Printing DIV tag using JavaScript



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

       <head>
              <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
              <title>Printing DIV Tag Example</title>
              <script type="text/javascript">
                  function PrintResults() {
                      var elem = document.createElement("div");
                      elem.innerHTML = "<br />";

                      var content1 = elem.innerHTML;
                      var content2 = document.getElementById("DivToPrint").innerHTML;

                      var win1 = window.open("", "Print_Results", "scrollbars=yes,width=800,height=700,toolbar=no,resizable=true,status=false,location=false");

                      win1.document.open();

                      win1.document.write("<html>"
                                             + "<head><title>Header</title></head>"
                                             + "<body onload='window.print();window.close();'>"
                                          + content1 + content2 +
                                             "</body></html>");

                      win1.document.close();
                  }
              </script>
       </head>

       <body>
                     <div>
                           This is non printing zone....<br />
                           <br />
                     </div>
                     <div id="DivToPrint">
                           <br />
                           This is printing zone...<br />
                           This is printing zone...<br />          
                           This is printing zone...<br />
                           This is printing zone...<br />
                           This is printing zone...<br />
                           This is printing zone...<br />
                     </div>
                     <button onclick="PrintResults()">Print</button>
       </body>

</html>

Updating Items in List in SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project. (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces.
Step 3: Write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//SharePoint namespace
using Microsoft.SharePoint.Client;

namespace UpdatingListItem
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;

            //Getting List
            List list = site.Lists.GetByTitle("Students");

            //Getting List item
            ListItem listItem = list.GetItemById(2);

            //Updating List item
            listItem["First Name"] = "Hrithik";
            listItem["Last Name"] = "Roshan";
            listItem["Age"] = 37;
            listItem.Update();

            clientContext.ExecuteQuery();
            Console.WriteLine("List Item Updated Successfully");
            Console.ReadLine();
        }
    }
}


Filtering Items from List in SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project. (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces.
Step 3: Write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//SharePoint namespace
using Microsoft.SharePoint.Client;

namespace FilteringListItems
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;

            //Getting List
            List list = site.Lists.GetByTitle("Students");
            clientContext.Load(list);
            clientContext.ExecuteQuery();

            //Filtering List Items
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml =
                @"<View>
                    <Query>
                      <Where>
                        <Eq>
                          <FieldRef Name='Age' />
                          <Value Type='Number'>35</Value>
                        </Eq>
                      </Where>
                    </Query>
                  </View>";
            ListItemCollection listItemCollection = list.GetItems(camlQuery);
            clientContext.Load(listItemCollection);
            clientContext.ExecuteQuery();

            //Printing List Items
            foreach (ListItem listItem in listItemCollection)
            {
                Console.WriteLine("Title: {0}, First Name: {1}, Last Name: {2}, Age: {3}", listItem["Title"], listItem["First Name"], listItem["Last Name"], listItem["Age"]);
            }
            Console.ReadLine();
        }
    }
}


Getting Items from List in SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project. (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces.
Step 3: Write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//SharePoint namespace
using Microsoft.SharePoint.Client;

namespace ReadingListItems
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;

            //Getting List
            List list = site.Lists.GetByTitle("Students");
            clientContext.Load(list);
            clientContext.ExecuteQuery();

            //Getting List Items
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View/>";
            ListItemCollection listItemCollection = list.GetItems(camlQuery);
            clientContext.Load(listItemCollection);
            clientContext.ExecuteQuery();

            //Printing List Items
            foreach (ListItem listItem in listItemCollection)
            {
                Console.WriteLine("Title: {0}, First Name: {1}, Last Name: {2}, Age: {3}", listItem["Title"], listItem["First Name"], listItem["Last Name"], listItem["Age"]);
            }
            Console.ReadLine();
        }
    }
}

Deleting Items from List in SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project. (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces.
Step 3: Write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//SharePoint namespace
using Microsoft.SharePoint.Client;

namespace DeletingListItems
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;

            //Getting List
            List list = site.Lists.GetByTitle("Students");

            //Deleting List Item
            ListItem listItem = list.GetItemById(2);
            listItem.DeleteObject();
            clientContext.ExecuteQuery();

            Console.WriteLine("List Item Deleted Successfully");
            Console.ReadLine();
        }
    }
}