Read Sharepoint Documents using c# code

class Program
{
static void Main(string[] args)
{
GetFiles();
}

public static void GetFiles()
{
ClientContext clientContext = new ClientContext(“http://ipaddress:8080/”);
clientContext.Credentials = new NetworkCredential(“username”, “pass”);

Web web = clientContext.Web;
clientContext.Load(web);
clientContext.Load(web.Lists);
clientContext.Load(web, wb => wb.ServerRelativeUrl);
clientContext.ExecuteQuery();

List list = web.Lists.GetByTitle(“Shared Documents”);
clientContext.Load(list);
clientContext.ExecuteQuery();

Folder folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + @”Shared Documents\1″);
clientContext.Load(folder);
clientContext.ExecuteQuery();

CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @”

“;
camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
FileInformation fileInformation;

foreach (var item in listItems)
{
clientContext.Load(item.File);
clientContext.ExecuteQuery();

fileInformation = File.OpenBinaryDirect(clientContext, (string)item.File.ServerRelativeUrl);
using (System.IO.StreamReader sr = new System.IO.StreamReader(fileInformation.Stream))
{
// Read the stream to a string, and write the string to the console.
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
}
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s