Hi All,
I received a recent request to provide a tool to upload documents from a local machine to sharepoint document library using client object model in SPS 2010. I have selected the managed code model to do it.
This tool takes 3 inputs:
1. The URL of the sharepoint site where the library is located.
2. The name of the document library.
3. Path of the folder in your local machine.
If the folder does not exists it will create a new folder and if the files already exists it will overwrite them.
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.SharePoint.Client;
namespace TestClientManagedCode
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("This tool will copy the entire folder from your local machine to the sharepoint document library");
Console.WriteLine("Please enter the url of the site");
String siteUrl = Console.ReadLine();
Console.WriteLine("Please enter the name of the document library");
String dLibraryName = Console.ReadLine();
Console.WriteLine("Please enter the full path of the folder containing the files without the filname");
String folderPath = Console.ReadLine();
try
{
using (ClientContext ctx = new ClientContext(siteUrl))
{
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
List dUplaod = web.Lists.GetByTitle(dLibraryName);
String[] fileNames = Directory.GetFiles(@folderPath);
bool exists = false;
DirectoryInfo dInfo = new DirectoryInfo(@folderPath);
FolderCollection folders = dUplaod.RootFolder.Folders;
char[] sep = { '\\' };
ctx.Load(folders);
ctx.ExecuteQuery();
foreach (Folder eFolder in folders)
{
if (eFolder.Name.Equals(dInfo.Name))
{
foreach (String fileName in fileNames)
{
String[] names = fileName.Split(sep);
FileCreationInformation fCInfo = new FileCreationInformation();
fCInfo.Content = System.IO.File.ReadAllBytes(fileName);
fCInfo.Url = names[names.Length - 1];
eFolder.Files.Add(fCInfo);
exists = true;
}
}
}
if (!exists)
{
Folder tFolder = folders.Add(siteUrl + "/" + dLibraryName + "/" + dInfo.Name);
foreach (String fileName in fileNames)
{
String[] names = fileName.Split(sep);
FileCreationInformation fCInfo = new FileCreationInformation();
fCInfo.Content = System.IO.File.ReadAllBytes(fileName);
fCInfo.Url = names[names.Length - 1];
tFolder.Files.Add(fCInfo);
}
}
ctx.ExecuteQuery();
Console.WriteLine("The Execution is completed");
Console.ReadLine();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}
}
I received a recent request to provide a tool to upload documents from a local machine to sharepoint document library using client object model in SPS 2010. I have selected the managed code model to do it.
This tool takes 3 inputs:
1. The URL of the sharepoint site where the library is located.
2. The name of the document library.
3. Path of the folder in your local machine.
If the folder does not exists it will create a new folder and if the files already exists it will overwrite them.
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.SharePoint.Client;
namespace TestClientManagedCode
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("This tool will copy the entire folder from your local machine to the sharepoint document library");
Console.WriteLine("Please enter the url of the site");
String siteUrl = Console.ReadLine();
Console.WriteLine("Please enter the name of the document library");
String dLibraryName = Console.ReadLine();
Console.WriteLine("Please enter the full path of the folder containing the files without the filname");
String folderPath = Console.ReadLine();
try
{
using (ClientContext ctx = new ClientContext(siteUrl))
{
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
List dUplaod = web.Lists.GetByTitle(dLibraryName);
String[] fileNames = Directory.GetFiles(@folderPath);
bool exists = false;
DirectoryInfo dInfo = new DirectoryInfo(@folderPath);
FolderCollection folders = dUplaod.RootFolder.Folders;
char[] sep = { '\\' };
ctx.Load(folders);
ctx.ExecuteQuery();
foreach (Folder eFolder in folders)
{
if (eFolder.Name.Equals(dInfo.Name))
{
foreach (String fileName in fileNames)
{
String[] names = fileName.Split(sep);
FileCreationInformation fCInfo = new FileCreationInformation();
fCInfo.Content = System.IO.File.ReadAllBytes(fileName);
fCInfo.Url = names[names.Length - 1];
eFolder.Files.Add(fCInfo);
exists = true;
}
}
}
if (!exists)
{
Folder tFolder = folders.Add(siteUrl + "/" + dLibraryName + "/" + dInfo.Name);
foreach (String fileName in fileNames)
{
String[] names = fileName.Split(sep);
FileCreationInformation fCInfo = new FileCreationInformation();
fCInfo.Content = System.IO.File.ReadAllBytes(fileName);
fCInfo.Url = names[names.Length - 1];
tFolder.Files.Add(fCInfo);
}
}
ctx.ExecuteQuery();
Console.WriteLine("The Execution is completed");
Console.ReadLine();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}
}
I hope this will help you out.
Thanks,
Rahul Rashu
No comments:
Post a Comment