none
Read a File in document library

    Question

  •  

    Hi All,

     

    Can one help me how to write a piece of code in C# to get/read a file in the document library of MOSS 2007..

     

    Thanks in advance.

     

    Munna

    Wednesday, April 02, 2008 8:46 PM

Answers

  • Here is an example that you can start with:

     

    Code Snippet

    string contents = string.Empty;

    using (SPSite site = new SPSite("http://servername/"))

    {

    using (SPWeb web = site.RootWeb)

    {

    SPFile file = web.Folders["Documents"].Files["filename.txt"];

    using (StreamReader reader = new StreamReader(file.OpenBinaryStream()))

    {

    contents = reader.ReadToEnd();

    }

    }

    }

     

     

    Wednesday, April 02, 2008 9:09 PM

All replies

  • Here is an example that you can start with:

     

    Code Snippet

    string contents = string.Empty;

    using (SPSite site = new SPSite("http://servername/"))

    {

    using (SPWeb web = site.RootWeb)

    {

    SPFile file = web.Folders["Documents"].Files["filename.txt"];

    using (StreamReader reader = new StreamReader(file.OpenBinaryStream()))

    {

    contents = reader.ReadToEnd();

    }

    }

    }

     

     

    Wednesday, April 02, 2008 9:09 PM
  • Hi ,

     

    Thanks for your response.

     

    I would like to get a filename "xx.html" in the document library folder and verify "sometext" in the file.

     

    Please let me know if there is any code that will help me.

     

    Thanks in advance,

     

    Thursday, April 03, 2008 1:07 PM
  • This should be what you need:

     

    Code Snippet

    string contents = string.Empty;

    using (SPSite site = new SPSite("http://servername/"))

    {

    using (SPWeb web = site.RootWeb)

    {

    SPFile file = web.Folders["Documents"].Files["xx.html"];

    using (StreamReader reader = new StreamReader(file.OpenBinaryStream()))

    {

    contents = reader.ReadToEnd();

    }

    }

    }

    bool contains = contents.IndexOf("sometext") > 0;

     

     

     

    Thursday, April 03, 2008 4:46 PM
  • Hi George,

     

    Thanks alot.i will check the code and let you know if i need any help.

     

    can you please give me your email id , so that i can send you mail.

     

    Thanks,

     

    Thursday, April 03, 2008 6:56 PM
  • Hi George,

     

    I have different languages of the xx.html file in the sub folder where the code you provided is not working.

     

    The complete reqs are AAA document library -->BBB,CCC,DDD etc subfolder and in each sub folder i have xx.html files.

     

    I want to read a xx.html files from subfolders.

     

    If any one folder not having xx.html file then make default one is xx.html file from BBB subfolder.

     

    Please provide any code snippet for this.

     

    Thanks for your time.

     

    Monday, April 07, 2008 2:34 PM
  •  

    There's a good example of traversing sharepoint list folder hierarchies.

     

    http://blog.krichie.com/2007/01/30/traversing-sharepoint-list-folder-hierarchies/

     

    I hope this helps.

    Tuesday, April 08, 2008 12:11 AM
  • This is what I need, but I cant get it to work. I get the following error:

    "Request for the permission of type 'Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' failed."

    I am using SmartPart Web Part project type (from codeplex). My other WebParts have not this problem. It has something to do with "SPSite" I think.

    Settings from web.config:
    <SafeControl Assembly="SmartTest" Namespace="SmartTest" TypeName="*" Safe="True" />

    Need more information?

    //Micael
    Wednesday, October 01, 2008 3:21 PM
  • Here is an example that you can start with:

     

    Code Snippet

    string contents = string.Empty;

    using (SPSite site = new SPSite("http://servername/"))

    {

    using (SPWeb web = site.RootWeb)

    {

    SPFile file = web.Folders["Documents"].Files["filename.txt"];

    using (StreamReader reader = new StreamReader(file.OpenBinaryStream()))

    {

    contents = reader.ReadToEnd();

    }

    }

    }

     

     Hi

    Your code is working fine.I want to show excel file data in Visual web part .Excel file is in SharePoint Document Library.I am using sp2013.

    I used using Microsoft.Office.Interop.Excel;

    and Application Class,it throws error

    Can u plz help me?


    Monday, February 03, 2014 6:02 AM