Recursively update files & folders to a SharePoint Library
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
private void button1_Click(object sender, EventArgs e)
{
string strSourcePath = txtLocalSource.Text;
string strSiteUrl = txtServerUrl.Text;
string strLibraryName = txtLibraryName.Text;
try
{
using (SPSite site = new SPSite(@strSiteUrl))
{
SPWeb Web = site.OpenWeb();
UploadORCreateDirectories(strSourcePath,Web.Folders[strLibraryName].SubFolders);
}
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
//function
private void UploadORCreateDirectories(string filePath, SPFolderCollection folderCollection)
{
//Upload all files
foreach (FileInfo fi in new DirectoryInfo(filePath).GetFiles())
{
FileStream fileStream = File.OpenRead(fi.FullName);//Read stram
SPFile spfile = folderCollection.Folder.Files.Add(fi.Name, fileStream, true);
spfile.Update();
}
//Upload Multiple Folders
foreach (DirectoryInfo di in new DirectoryInfo(filePath).GetDirectories())
{
string subFolderName = di.FullName.Split('\\')[di.FullName.Split('\\').Length - 1];
SPFolder spNewFolder = folderCollection.Add(subFolderName);
spNewFolder.Update();
//Recursive call to create sub folders
UploadORCreateDirectories(di.FullName, spNewFolder.SubFolders);
}
}
No comments:
Post a Comment