SharePoint 2007 - Read content from SPFile

Thursday, 5 March 2009 10:41 by myro

If you need to read from a file (ex txt, html) located in SharePoint Document library, you need to get the SPFile object and then call the OpenBinaryStream from that file. here you can find a simple example that shows how to read from a html/txt file uploaded in a SharePoint Document library (it works too with SPLists):

string content = string.Empty;
      using (SPSite oSite = new SPSite("http://localhost/"))
      {
        using (SPWeb oWeb = oSite.OpenWeb())
        {
          SPDocumentLibrary doclib = (SPDocumentLibrary)oWeb.GetList(DocLibUrl);
          SPFile htmlFile = doclib.Items[0].File;
          using (System.IO.StreamReader reader = new System.IO.StreamReader(htmlFile.OpenBinaryStream()))
          {
            content = reader.ReadToEnd();
          }
        }
      }

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   , ,
Categories:   SharePoint 2007
Actions:   Bookmark and Share | Permalink | Comments (6) | Comment RSSRSS comment feed
If you consider this post usefull for your purposes, please consider visiting my sponsors to help me out with the myrocode.com maintenance. Thank you.

Comments