Read CSV file from SharePoint document library programmatically using CSOM

SharePoint online Read csv file from document library using .Net managed client object model csom
SharePoint deveopment training course

This SharePoint csom tutorial explains, how to read CSV file from document library using .Net client object model (csom) in SharePoint Online.

Here we are going to write our csom code in a windows application. So first create a windows application and then add the below ddls in the reference. You can add these two dlls from NuGet package manager.

  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.Client.Runtime

Here we have a csv file which has 3 columns as:

  • SiteName
  • SiteURL
  • SiteDescription

And it has 3 rows of data and the CSV file looks like below:

sharepoint read csv file programmatically

And we have uploaded the CSV file to a SharePoint document library (MyDocLib) like below:

sharepoint read csv file programmatically c#

Read CSV file from SharePoint document library using CSOM

Below is the full code to read the CSV file from the SharePoint document library using csom.

private void btnSubmit_Click(object sender, EventArgs e)
{
using (ClientContext context = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
string username = "********@onlysharepoint2013.onmicrosoft.com";
context.AuthenticationMode = ClientAuthenticationMode.Default;
var secureString = new SecureString();
foreach (char c in ""********")
{
secureString.AppendChar(c);
}
context.Credentials = new SharePointOnlineCredentials(username, secureString);
try
{
DataTable dt= ImportSiteList(context);
}
catch (Exception ex)
{
throw;
}
}
}

private DataTable ImportSiteList(ClientContext context)
{
try
{
List list = context.Web.Lists.GetByTitle("MyDocLib");
context.Load(list);
context.ExecuteQuery();
Folder folder = list.RootFolder;
FileCollection files = folder.Files;
context.Load(files);
context.ExecuteQuery();
DataTable dt = new DataTable("tblSiteLists");
foreach (Microsoft.SharePoint.Client.File f in files)
{
FileInformation fileInformation = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, (string)f.ServerRelativeUrl);
if((string)f.ServerRelativeUrl == "/sites/Bhawana/MyDocLib/Sites.csv")
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(fileInformation.Stream))
{
String line = sr.ReadToEnd();
string dataformat = line.Replace("\r\n", ",");
string[] strArray = dataformat.Split(‘,’);
dt.Columns.Add("SiteName", typeof(string));
dt.Columns.Add("SiteURL", typeof(string));
dt.Columns.Add("SiteDescription", typeof(string));
int j = 0;
DataRow dr = dt.NewRow();
for (int i = 3; i < strArray.Length – 1; i++)
{
dr[j] = strArray[i];
j++;
if (j == 3)
{
j = 0;
dt.Rows.Add(dr);
dr = dt.NewRow();
}
}
}
}
}
return dt;
}
catch (Exception ex)
{
throw ex;
}
}

The above code will return a data table which looks like below:

sharepoint read csv file programmatically csom

You may like following SharePoint csom tutorials:

This SharePoint tutorial, we learned how to read CSV file from SharePoint document library programmatically using CSOM.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

View all posts by Bhawana Rathore →