Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Programatically Add Files And Folder In SharePoint DocumentLibrary

Article:
Viewed:  2783 
Posted On:  11/06/2013 09:07:47 
How to Programatically add file in folder in sharepoint Document Library..? 

In my previous article I explained how we can take backup of a document library from SharePoint site to local drive in same hierarchy as it's on document library.

http://www.dotnetcodesg.com/Article/UploadFile/2/173/Copy%20Files%20from%20document%20library%20to%20local%20in%20SharePoint.aspx

Now in this article I am going to explain how we can add files and folder from local machine to SharePoint document library by keeping same hierarchy as it's in document library.

Below is my code to achieve this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.SharePoint;
 
namespace Add_Files_And_Folder_InDocumentLibrary
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo(@"F:\BKP\DOTNETCODESG_TEST\");
            CopyDirectory(@"DOTNETCODESG_TEST\");
        }
 
        static void UploadFile(string SourcePath, string DestPath)
        {
            string FileUrl = string.Empty;
            string Folder = string.Empty;
            string filename = string.Empty;
            string destPath = @"F:\BKP\DOTNETCODESG_TEST\";
            try
            {
                FileUrl = "http://localhost:2000/";
                Folder = "DOTNETCODESG_TEST"; 

                if (File.Exists(SourcePath))
                {
                    FileStream fStream = File.OpenRead(SourcePath);
                    byte[] contents = new byte[fStream.Length];
                    fStream.Read(contents, 0, (int)fStream.Length);
                    fStream.Close();
                    SPSite site = new SPSite(FileUrl);
                    SPWeb web = site.RootWeb;
                   
                    SPFileCollection destFiles = web.GetFolder(DestPath).Files;
                    filename = GetFileName(SourcePath);
                    destFiles.Add(filename, contents, true);
                }
            }
            catch (Exception ex)
            {
            } 

        } 

        static string GetFileName(string url)
        {
            int LastIndex = url.LastIndexOf("\\");
            url = url.Substring(LastIndex + 1, (url.Length - LastIndex - 1));
            return url;
        } 

        static void CopyDirectory(string Path)
        {
            DirectoryInfo source = new DirectoryInfo(@"F:\\BKP\\" + Path);
            string FileUrl = string.Empty;
            string Folder = string.Empty;
            FileUrl = "http://localhost:2000/";
            Folder = Path; 

            // Copy all files.
            FileInfo[] files = source.GetFiles();
            foreach (FileInfo file in files)
            {
                UploadFile(source.FullName + @"\" + file.Name, FileUrl + Folder);                
            } 

            // Process subdirectories.
            DirectoryInfo[] dirs = source.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {
                int len = dir.ToString().LastIndexOf('/');
                string folderName = dir.ToString().Substring(len + 1);
                SPWeb web = new SPSite("http://localhost:2000").OpenWeb();
                using (web)
                {
                    SPFolderCollection folders = web.GetFolder(Path).SubFolders;
                    folders.Add(folderName);
                }               
                CopyDirectory(Path + dir.Name + @"\");
            }
        }
    }
}

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us