Wednesday, August 31, 2011

Uploading Files to moss 2007 using WinForm

Hi Guys my client wanted application which can upload files from client folder to MOSS 2007 Site Library

Scenario:- When client past any file in a Folder call XYZ File upload should start!
Conclusion :- Apply File Watcher on folder XYZ folder so that all the activities can be traced

Code for Reference :-
//Attach Watch on particular folder
FileSystemWatcher oFileSystemWatcher = new FileSystemWatcher("D:\\redhatabhi\\xyz\\");
 //add watch on folder when New file is Created/Pasted/Moved
oFileSystemWatcher.Created += new FileSystemEventHandler(oFileSystemWatcher_Created);
// use following Property to Enable Event Raise in when new file are been added
oFileSystemWatcher.EnableRaisingEvents = true;
// use following Property for including Sub-folders also
oFileSystemWatcher.IncludeSubdirectories = true;

After applying this event you job is now to upload File in MOSS 2007.
Example :-
            FileInfo file = new FileInfo(e.FullPath);
            if (file.Extension == ".exe")
            notify.ShowBalloonTip(100, "Warning", "Uploading of Executable File is Not Allowed", ToolTipIcon.Warning);          
             using (SPSite oSPSite = new SPSite(siteURL))
            {
                using (SPWeb oSPWeb = oSPSite.OpenWeb())
                {
                    SPFolder oSPFolder = oSPWeb.GetFolder(oSPWeb.Url + "/DocumentLibrary/");
                    oSPFolder.Files.Add(file.Name, File.ReadAllBytes(file.FullName), true);
                    file.Delete();
                }        
             }

No comments:

Post a Comment