Pages

Saturday, November 21, 2009

SharePoint: Download document Library file From Remote Client.

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"];

if (string.IsNullOrEmpty(domain))

{

client.Credentials = new NetworkCredential(userName, password);

}

else

{

client.Credentials = new NetworkCredential(userName, password, domain);

}

string urlOfTheSPFile = "http://spServer.com/mysite/Docs/file.doc";

string localPathToTheFile = @"d:\DownloadedFiles\file.doc";

client.DownloadFile(urlOfTheSPFile, localPathToTheFile);

client.Dispose();

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.

3 comments:

  1. "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." -- You're right! I spent a while dealing with a built in sharepoint solution, but this become to my solution in a SQL SSIS project. The weakness point would be in case where you don't have a generic domain account for SSIS solutions, otherwise this is a really simple solution!

    ReplyDelete

Note: Only a member of this blog may post a comment.