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

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

Here we will discuss how we can read csv file from document library using .Net client object model (csom) in SharePoint online (Download PDF for FREE). Here we are going to write our code in a windows application. So first create a windows application and then add the below ddls in the reference.

– Microsoft.SharePoint.Client
– Microsoft.SharePoint.Client.Runtime

SharePoint deveopment training course

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 online read csv file net client object model
sharepoint online read csv file net client object model

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

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

Below is the full code to read the csv file from document library.



private void button5_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;
}
}

Read some sharepoint online tutorials:


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

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

Hope this will be helpful.

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 →