Blog about Microsoft technologies, Office System, SharePoint, MOSS, WSS, Search and "The new world of work"
in

Getting / Downloading a file from a SharePoint Library by using WebClient

The following code can retrieve a file from a SharePoint library by suing the WebClient class. You will need the credentials of the SharePoint site to create a connection. The file is retrieved as binary and stored into a local file. Eventually you get an FileStream object back pointing to the downloaded file.

  byte[] binFile = null;
  System.IO.FileStream fs = null;
  System.IO.BinaryReader rdr = null;
  string tempFileName = "C:\\tmp";
  string url =
http://portal.demo.nl/lists/doclib/mydoc.docx;

The following retrieves the document based on the url field and some credentials:

  try
  {
    System.Net.WebClient client = new System.Net.WebClient();
    client.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
    client.DownloadFile(url, tempFileName);

    System.IO.FileInfo fInfo = new System.IO.FileInfo(tempFileName);
    long numBytes = fInfo.Length;
    fs = new System.IO.FileStream(tempFileName, System.IO.FileMode.Open);
  }
  catch (System.Net.WebException ex1)
  {
    // catch the web exception
    fs.Close();
  }
  catch (Exception ex)
  {
    // catch the default exception
    fs.Close();
  }

» Trackbacks & Pingbacks

  1. Hacía tiempo que no revisaba mis RSS sobre SharePoint , y claro me ha costado ponerme al día, y aquí

    Blog del CIIN — February 26, 2008 1:00 AM
  2. Effects of lexapro. Lexapro.

    Lexapro and alcohol. — August 25, 2008 8:59 PM
Trackback url for this post:
http://www.bloggix.com/blogs/microsoft/trackback.ashx?PostID=225

» Comments

    No comments yet. Be the first to comment!

» Leave a Comment

(required) 
(optional)
(required) 

Submit

Subscriptions

News

SlideShare.net
I'm using SlideShare.net for sharing my presentation material. You can find it here

Dutch SharePoint User Group
The Dutch SharePoint User Group is an independent social network for people to collaborate and share information around SharePoint Technology and products. By learning from others you can improve your skills and expertise, around areas, which are then again a benefit to the social network. This platform is a community platform for SharePoint developers, architects and designer who want to share information, tricks, code and content. Its also a good starting point for people without SharePoint knowledge who are interested in SharePoint Technology. Read more

Oktober 2008 i'm working for a new company called Sparked. The new world of work causes organizations to re-think about their ways of communication and interaction with their customers.

Increasing usage of web technology and the new generation workers brings changes into these organizations.

Sparked helps these organizations to accelerate adoption of this technology platform to effectively use it around content management, content exposure and collaboration.


April 2008 i have been to the first SharePoint Conference in Dubai. Dubai is an overwhelming town with a lot of passionate people. I had a great time. Thank you my friends!



You can also find my on Facebook

Tags

Alexander Meijers

Previous Blog about SharePoint