I was working with Sync feature of document library. In this feature, a user can Sync document library to the local machine.
What I am looking for is if there is a way to do the same with Programmatically(C# or other language)
Happy coding :)
As @Amy_MSFT states, this is complex. I was given this exact task, and after some research, here is what my approach was.
I wanted something that exactly simulates clicking the "Sync" button for a library. So, I knew I had to generate the sync links, and either force the click event, or have the user click a link to kick off the sync.
The link behind that "Sync" button can be built using this article: How to Manually Create a OneDrive Sync Link from Your SharePoint Page
I wrote an SPFx webpart using typescript/React that pulls all of the libraries for the given site and then generates link buttons to be clicked to sync each library. What I've found is the One Drive application can only handle one sync action at a time, so auto-clicking these links with code didn't work. If you find a way around this, I'd love to hear about it.
The user experience isn't quite what I wanted, but it's certainly a lot quicker and easier than going to each library to sync.
If you want to achieve the sync feature programmatically, it is complex. It will upload and download the files many times.
You could use OneDrive app to sync documents from SharePoint online to local machine.
More references:
Upload File to SharePoint Office 365 Programmatically using C# CSOM – PNP.
Download file from SharePoint Online.
Updated:
Yes, you can create the folder programmatically and set the folder as sync folder in OneDrive app.
More reference:
I watched this youtube video and then I wrote a PowerShell script that runs when the user signs in. It is working ok... although, it could be better.