Say you have deployed your SharePoint on a pc www.sharepointserver.com. And you are accessing the SharePoint site from your local pc named as clientpc. Now if you need to download a file from SharePoint document library then how will do that? Let’s find if there’s any SharePoint built-in solution.
Has any SharePoint Solution?
At first you will try to find any built in web service in SharePoint. At least I did so and I spent few hours to find out if there’s any web service in SharePoint to download the file. I found one which is Copy.asmx. But copy.asmx will work only if source and destination url in the same web application or site collection. So if you use the copy.asmx file to download file in the same server then it’ll work fine.But if you try to use it from remote pc then you’ll get exception.
Non-SharePoint (but asp.net) Solution
SharePoint is finally asp.net application. So anyone can expect asp.net experience will help anyway. In asp.net you can use System.Net.WebClient to download file from web server. The same logic can be used to download file from SharePoint considering it as an asp.net application.
System.Net.WebClient client = new System.Net.WebClient();
string userName = ConfigurationManager.AppSettings["SPUserName"];
string password = ConfigurationManager.AppSettings["SPPassword"];
string domain = ConfigurationManager.AppSettings["SPDomain"];
client.Credentials = new NetworkCredential(userName, password);
client.Credentials = new NetworkCredential(userName, password, domain);
string urlOfTheSPFile = "http://spServer.com/mysite/Docs/file.doc";
string localPathToTheFile = @"d:\DownloadedFiles\file.doc";
In the above code snippet the WblClient.DownloadFile method will take the url of the file and local path of the file where the file will be saved. I have not tested if its possible to upload file with WebClient but I think it should be.