Effortlessly Upload Files to a SharePoint Document Library Subfolder using C# Console Application in 2023
In the ever-evolving landscape of technology, SharePoint remains a powerful and widely used tool for document management and collaboration. This article explores how to effortlessly upload files to a SharePoint Document Library subfolder using a C# Console Application in 2023.
<h3>Context and Key Concepts</h3>
<p>SharePoint is a web-based collaborative platform that integrates with Microsoft Office. A Document Library is a storage location for files, where each file is represented as an item. Subfolders within a Document Library help keep related files organized and maintain a logical folder hierarchy.</p>
<p>To upload files programmatically, you'll be using the SharePoint Client Object Model (CSOM), which allows you to access SharePoint data and functionalities through a .NET Framework or .NET Core application. CSOM is available through the SharePoint Online Client Components SDK.</p>
<h3>Prerequisites</h3>
<p>
<ul>
<li>Visual Studio or another .NET IDE</li>
<li>SharePoint Online Client Components SDK</li>
<li>A SharePoint site with necessary permissions</li>
</ul>
</p>
<h3>Uploading Files</h3>
<p>First, set up your C# Console Application. Update NuGet packages, and install the Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime packages. Then, follow these steps:</p>
<pre><code>
using Microsoft.SharePoint.Client; using System; using System.IO;
class Program
{
static void Main(string[] args)
{
// SharePoint site URL and credentials
string siteUrl = "https://[your_sharepoint_site_url]";
string username = "[your_username]";
string password = "[your_password]";
// Relative path to the target subfolder
string subfolderPath = "/Documents/MySubFolder";
// File upload path
string filePath = @"C:\path\to\your\file.txt";
// Authenticate to SharePoint Online
ClientContext clientContext = new ClientContext(siteUrl);
SecureString securePassword = new SecureString();
foreach (char c in password.ToCharArray())
{
securePassword.AppendChar(c);
}
clientContext.Credentials = new SharePointOnlineCredentials(username, securePassword);
// Get the target subfolder's URL
Web web = clientContext.Web;
Folder targetFolder = web.GetFolderByServerRelativeUrl(subfolderPath);
clientContext.Load(targetFolder);
clientContext.ExecuteQuery();
// Upload the file
FileCreationInformation fileCreation = new FileCreationInformation
{
Content = System.IO.File.ReadAllBytes(filePath),
Url = Path.GetFileName(filePath)
};
targetFolder.Files.Add(fileCreation);
targetFolder.Update();
clientContext.ExecuteQuery();
Console.WriteLine("File successfully uploaded!");
Console.ReadLine();
}
}
</code></pre>
<p>Uploading files to a SharePoint subfolder using C# in 2023 involves setting up a C# Console Application,
installing the Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime packages, and using the SharePoint Client Object Model (CSOM) to authenticate to the SharePoint site, access the target subfolder, and upload files. With these steps, you can save time and effort sharing documents with your team through SharePoint.</p>
<h2>References</h2>
<ul>
<li><a href="https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/sharepoint-client-object-model-overview">SharePoint Client Object Model Overview</a></li>
<li><a href="https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code-in-
csharp#upload-a-file-to-a-document-library-by-using-code">Upload a file to a document library by using code</a></li>
</ul>