Introduction
In this article we will be explaining how to upload Files and documents to a Sharepoint remote Site using C# under .Net Framework 4.x, we will be mentioning the way to create, delete directories & files as well.
Step 1: Add references to Microsoft.SharePoint
First of all you will need to Install the references through NuGet using this command:
Install-Package Microsoft.SharePoint.Client
Doing that will add reference in you project references to these assemblies:
Microsoft.SharePoint.Client
Microsoft.SharePoint.Client.Runtime
Add the following using
’s to your C# code file:
using Microsoft.SharePoint.Client
using Microsoft.SharePoint.Client.Runtime
Step 2: Basic components
private class Dir
{
public string name;
public List<string> documents;
}
-
Dir: is simply the class we use to assist us creating a series of Documents under a given Directory. The Dir class is structured as follows:
-
name : The full path of the directory to create.
-
documents : Documents contained within this Directory, this is the list of “absolute” local file paths to upload to SharePoint, these aren’t necessarily required to be located within the directory “name” of the Dir class.
Step 3: SharePoint Export Method
private static void exportBatchSharepoint(string website, string username, string password, string path, List<Dir> subdirs)
{
Folder bfolder;
try
{
if (string.IsNullOrEmpty(website))
throw new Exception("Error: Sharepoint Not configured correctly.");
using (Microsoft.SharePoint.Client.ClientContext client = new ClientContext(website))
{
System.Diagnostics.Debug.WriteLine("Connecting to Sharepoint site...");
client.Credentials = new System.Net.NetworkCredential(username, password);
System.Diagnostics.Debug.WriteLine("Connected.");
var root_folder = client.Web.GetFolderByServerRelativeUrl(path);
if (root_folder == null)
root_folder = client.Web.Folders.Add(path);
System.Diagnostics.Debug.WriteLine("\tCreating Sharepoint Sub-Directory \"" + path + "\".");
foreach (var dir in subdirs)
{
string dir_path = Uri.EscapeUriString(dir.name).Replace("?", "_");
System.Diagnostics.Debug.WriteLine("\t\tCreating Document Directory \"" + dir_path + "\".");
var dir_folder = root_folder.Folders.Add(dir_path);
foreach (var doc in dir.documents)
{
string doc_path = Path.GetFileName(doc);
var uplfileStream = System.IO.File.ReadAllBytes(doc);
dir_folder.Files.Add(new FileCreationInformation()
{
Content = uplfileStream,
Overwrite = true,
Url = doc_path
});
}
}
System.Diagnostics.Debug.WriteLine("\tUploading to Sharepoint Server is Done.");
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("ERROR: " + ex.Message);
}
}
The function above exports given List of Documents (files), into a specific Path within the SharePoint site, using the credentials supplied.
That’s some of the basics, and how-to get started with Sharepoint on C# Wiki Post, hope it’s of interest and helps anyone out there, more wiki posts will be written globally on the platform on regular basis.
> **Wiki Posts: can be edited by absolutely anyone, so you are welcome to add more details & information to this post.**