only My site

Monday, October 29, 2012

Read File content from Document Library

The below code finds the file NewProcess.js in "Shared Document" document library and read the content (java script code) from the javascript file. It opens the file in Binary strean and assigns to a StreamReader Object.

        SPWeb web = SPContext.Current.Web;
        SPList list = web.Lists["Shared Documents"];
        string content = string.Empty;
           
        SPQuery query = new SPQuery();
        query.Query = "<Where><Eq><FieldRef Name='FileLeafRef' />
            <Value Type='File'>NewProcess.js</Value></Eq></Where>";
        SPListItemCollection items = list.GetItems(query);
        SPListItem item = items[0];
        if (item.Name.Equals("NewProcess.js"))
        {
            using (System.IO.StreamReader reader =
                   new System.IO.StreamReader(item.File.OpenBinaryStream()))
            {
                content = reader.ReadToEnd();
            }
        }

No comments: