Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We are wanting to push our direct deposit remittance documents to a folder on SharePoint Online for our employees.
Is it possible to connect to SharePoint in C# and transfer those files to a particular folder in SharePoint?

What I have tried:

I'm not even sure where to start. I've been writing C# code for 14-15 years but I know almost nothing about SharePoint.
Posted
Updated 29-Jan-20 20:06pm

1 solution

Following code can help you
C#
ClientContext ctx = new ClientContext("http://SPSiteURL");

Folder folder = ctx.Web.GetFolderByServerRelativeUrl("Documents");
string file = String.Concat(Environment.CurrentDirectory, @"\Files\FileName.pdf");

List doclib = ctx.Web.Lists.GetByTitle("Documents");
ctx.Load(doclib);
ctx.ExecuteQuery();

using (MemoryStream stream = new MemoryStream(System.IO.File.ReadAllBytes(file)))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, "/Documents/SubFolder/spg.pdf", stream, true);
}
 
Share this answer
 
v2
Comments
tchris 20-Apr-22 12:37pm    
When I load this code into VS2022, it wants to download an AWS package for the ClientContext object. Is there a Microsoft package that can be used since SharePoint is a MS product?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900