Offline Document Uploader in SharePoint 2010

A week ago i was just exploring Sharepoint 2010 Client Object model and have developed a utility which transfers all the documents from the windows folder to the Sharepoint document library. 

Following are the steps to develop Offline Document uploader in SharePoint 2010 using Client Object Model.

1. Create a menu item which opens files uploader utility from windows folder’s context menu.

                                        

In order to create custom menu item in windows, we have to place an entry into registry. 

I have first defined the two constant string variables as shown below that contains path to create a menu in the registry. You might have noticed that i have mentioned “Folder” at starting in both the values. This means that i want to create a context menu item at folder level. Otherwise if i place ” * ” it will be created at files, folder etc.

private const string MenuName = "Folder\\shell\\CMenuOption";
private const string Command= "Folder\\shell\\CMenuOption\Command";

Following is a code snippet that creates menu item at folder level. You can place it on any event. I have created two textboxes on a windows form i.e. txtMenuName and txtExecutablePath. In the txtMenuName textbox, i’ve  specified menu item name and in the txtExecutablePath i’ve specified the .exe path of an application which transfers file to sharepoint library.

Here is the complete code which creates a menu item into the registry.
           
RegistryKey regmenu = null;
            RegistryKey regcmd = null;
            try
            {
                regmenu = Registry.ClassesRoot.CreateSubKey(MenuName);
                if (regmenu != null)
                    regmenu.SetValue("", this.txtMenuName.Text);
                regcmd = Registry.ClassesRoot.CreateSubKey(Command);
                if (regcmd != null)
                    regcmd.SetValue("", this.txtExecutablePath.Text+ " \"%1\"");
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.ToString());
            }
            finally
            {
                if (regmenu != null)
                    regmenu.Close();
                if (regcmd != null)
                    regcmd.Close();
            }

Now there is a separate console application which sends the files from the folder path to the SharePoint document library. Following is the code snippet.

 
static void Main(string[] args)
        {

            String filepath =  args[0].ToString();
            Console.WriteLine(filepath);
            Console.WriteLine("Processing Documents");
            String[] files = Directory.GetFiles(filepath);

            String docLibrary= System.Configuration.ConfigurationSettings.AppSettings.Get("DocLibrary").ToString();
            String SPSiteURL= System.Configuration.ConfigurationSettings.AppSettings.Get("SPSiteURL").ToString();

            ClientContext context = new ClientContext(SPSiteURL);
            foreach (String fileName in files)
            {
                using(FileStream fileStream= new FileStream(fileName,FileMode.Open))
                {
                    Console.WriteLine("Uploading file = " + fileName);
                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, "/"+ docLibrary + "/" + Path.GetFileName(fileName), fileStream, true);
                    Console.WriteLine("Uploaded");
                }
            }
 }

Finally, When the user clicks on “Upload files to SP” a console application runs and uploads all the documents in the Sharepoint document library.

                                                 

Figure shows the Console Application uploading documents into SP doc library.

                                                 

                                                           Figure shows the documents uploaded in the SP doc library.