Tuesday, February 5, 2013

Download files from Sharepoint Library Recursively

Hi
This is a small program that will download the contents of a Sharepoint documnt library.

This will download all the files in the subfolders as well

class Program
{
 static void Main(string[] args)
 {
  Console.WriteLine("This Application will download the documents in a library");
  Console.Write("Enter Site URL : ");
  string SiteURL = Console.ReadLine();

  Console.Write("Library Name : ");
  string LibraryName = Console.ReadLine();

  //Open the site using the given url
  using (SPSite site = new SPSite(SiteURL))
  {
   //Open the web application
   using (SPWeb web = site.OpenWeb())
   {
    try
    {
     //Get the library folder
     SPFolder folder = web.GetFolder(LibraryName);

     //Download files
     downloadFilesrecursively(folder);
    }
    catch (Exception ex)
    {
     //Print the details of the exception
     Console.WriteLine("An Error Occured while processing " + ex.Message);
    }
   }
  }

  Console.ReadLine();
 }

 //The method that downloads the files from a given SPFolder
 public static void downloadFiles(SPFolder folder)
 {
  foreach (SPFile item in folder.Files)
  {
   //Download the file
   byte[] fileData = item.OpenBinary();
   System.IO.FileStream fstream = System.IO.File.Create(item.Name);
   fstream.Write(fileData, 0, fileData.Length);
   Console.WriteLine(item.Name + " " + fileData.Length);
  }
 }

 //Method that is used to call the download method recursively
 public static void downloadFilesrecursively(SPFolder folder)
 {
  //If the folder have subfolders call the download
  //Methods for the subfolders
  if (folder.SubFolders.Count != 0)
  {
   //Download the files in the current folder
   downloadFiles(folder);
   foreach (SPFolder item in folder.SubFolders)
   {
    downloadFilesrecursively(item);
   }
  }
  else
  {
   //If the folder has no subfolders
   //then download the file in the folder
     downloadFiles(folder);   
  }
 }
}

Monday, February 4, 2013

SQLite Database in Windows 8 RT application Part-2

Hi Devs
This is the Part 2 of the post
You can find Part 1 Here

This post will have all the Coding required
Here I'll show you how to make a simple Employee Management system.

Hope you have the Project created in Part 1,
First goto the Project Properties->Build and Make the Platform target to x86 or x64 because Any CPU is not supported yet.

This is the UI of the sample application.

This will be the structure of the solution,


 Here I'm having two classes to manage the database
First the Employee Class
The concept is just like SQL CE you have to create a class to represent a table and the use LINQ to work with the database.
This class will be representation of the Employee table, the explanation is given as the comment in the code.

public class Employee
{
    /// <summary>
    /// The ID of the Employee
    /// Used as the primary key
    /// Automatically incremented
    /// </summary>
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }

    /// <summary>
    /// Name of the Employee
    /// </summary>
    [MaxLength(30)]
    public string Name { get; set; }

    /// <summary>
    /// Address of the Employee
    /// </summary>
    [MaxLength(30)]
    public string Address { get; set; }
}
 
Now the Employee Manager class which handles the interaction with the database

public class EmployeeManager
{
    /// <summary>
    /// The Connection to SQLite database file
    /// </summary>
    private SQLiteConnection connection;

    /// <summary>
    /// The constructor
    /// Creates the connection to the database and creates the table
    /// </summary>
    public EmployeeManager()
    {
        //Windows.Storage.ApplicationData.Current.LocalFolder.Path is used to get the location of the Application
        connection = new SQLiteConnection(Windows.Storage.ApplicationData.Current.LocalFolder.Path+"\\EmployeeDB");
        connection.CreateTable<Employee>();
    }

    /// <summary>
    /// Insert a new employee to the database
    /// </summary>
    /// <param name="employee">The Employee to be inserted</param>
    /// <returns>ID of the inserted employee</returns>
    public int insertEmployee(Employee employee)
    {
        return connection.Insert(employee);
    }

    /// <summary>
    /// Delete and employee from the database
    /// </summary>
    /// <param name="ID">ID of the employee to be deleted</param>
    public int deleteEmployee(int ID)
    {
        return connection.Delete<Employee>(ID);
    }

    /// <summary>
    /// Update the details of an employee
    /// </summary>
    /// <param name="employee">New Employee object</param>
    public int updateEmployees(Employee employee)
    {
        return connection.Update(employee);
    }

    /// <summary>
    /// Get a employee object
    /// </summary>
    /// <param name="ID">ID of the employee</param>
    /// <returns>Employee with the given ID</returns>
    public Employee findEmployee(int ID)
    {
        List<Employee> employees = connection.Table<Employee>().Where(X=>X.ID==ID).ToList();

        if (employees.Count()==1)
        {
            return employees.First();
        }
        else
        {
            return null;
        }
    }

    /// <summary>
    /// Get a list of all the employees in a database
    /// </summary>
    /// <returns></returns>
    public List<Employee> getAllEmployees()
    {
        return connection.Table<Employee>().ToList();
    }

}
 
Thats it you can just call these methods from the Button click events to use the database.
You can find the UI calls in the sample application

Sample Application

Happy Coding
Please Leave a comment
Guruparan G

SQLite Database in Windows 8 RT application Part-1

Hi Devs
This post is all about using a SQLIte database in Windows 8 application
In this post i'll explain how to setup Visual Studio 2012 in order to work with SQLite.
In Part 2 I'll explain how to do the basic operations such as Add delete and update.
Lets get started
First you'll have to install the SQLite runtime for windows.
Goto Tools->Extension and Updates

Then search for SQLite and install it

Create a project based on your requirement (Blank\Grid Application)
And now add the reference to your project


Here you'll have to add the reference to both C++ runtime and SQLite runtime because SQLite runtime requires C++ runtime.


Now we can move to the NuGet Packages
Here we'll be installing the Wrapper Class Library for SQlite.

Goto Tools->Library Package Manager->Manage NuGet Packages for Solution


And select the "Online" packages and search for Sqlite-net and install it


Now we are good to start coding
All the coding part will be covered in Part 2.

Please Leave a comment
Guruparan G