Forum Discussion

Dushan996's avatar
Dushan996
Copper Contributor
Jan 01, 2024

Uploading a file to a SharePoint Document Library Sub Folder using a C# Console Application in 2023

I've been scraping the Internet for two weeks using various different solutions given but none of them work in 2023. Most of the solutions provided are old and they do not work with the new libraries and also because of the added security features such as MFA. Also almost all the given solutions threw some kind of error and various people have suggested various different workarounds in comments to get those fixed. So I've been scanning these threads as well trying various different options but after failing, finally thought of posting it here so this would be useful to someone who is facing similar issues.

Why Console app: Because I want to use the Task Scheduler to automate this and run it once per day. Have used other means such as syncing with One Drive but it creates duplicate files every now and then. So thought of using a robust mechanism such as a C# console app but have tried PowerShell scripts as well hoping I will be able to ask the Task Scheduler to run PowerShell scripts on a schedule, but none of them worked.

Just to narrow it down
- I have disabled MFA
- Even tried using an App password (Yes I managed to generate the app passwords successfully, but won't work with C#, always some kind of error, after fixing one you get another but after some point you come to a point where there are no resources on how to fix those errors)
- I'm using SharePoint Developer site to test it out at the moment.

Example site URL could be: https://5mhhvs.sharepoint.com
Created a site called Documents: https://5mhhvs.sharepoint.com/sites/Documents
So my goal is to upload a file regularly to a sub folder in the Document Library of the Documents site.

Wondering if someone can provide a sample code, together with the package names I need to configure using NuGet and the necessary using statements (as there can be conflicting namespaces) either using username/password approach or app password (ClientID/Secret).

Other queries: If there is already a file, will it merge the changes or is there a setting we can configure so that it will merge the changes.
If a user has opened the file on SharePoint (and may be has started editing), will we still be able to upload the file or can we force it?

Hope someone will be able to put an end to end solution here instead of posting links to previous posts.
Thank you in advance.

4 Replies

Resources