Skip to content

Instantly share code, notes, and snippets.

Created August 12, 2015 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f135715cb2a7846795b1 to your computer and use it in GitHub Desktop.
Save anonymous/f135715cb2a7846795b1 to your computer and use it in GitHub Desktop.
public void LoopThroughExcel()
{
//getting datatable of excel rows by passing file name (from the form) and sheet name of excel
DataTable InternalRecords = ConvertExcelToDataTable(ofdBrowseFile.FileName, "SheetName");
//Looping through Excel records
foreach (DataRow row in InternalRecords.Rows)
{
try
{
foreach (DataColumn dtCol in InternalRecords.Columns)
{
//Check for file path column
if (dtCol.ColumnName.Contains("File Path"))
{
//get value of file path
if (!string.IsNullOrEmpty(row[dtCol.ColumnName].ToString()))
{
//Call upload document method. Upload file is of type SharePoint.Client.File
uploadFile = UploadDocument(row, fileName, oWeb, oList, row[dtCol.ColumnName].ToString());
}
}
}
}
}
//method to get datatable from excel
public DataTable ConvertExcelToDataTable(string filename, string tabName)
{
DataTable dataTable = null;
try
{
string sql = @"SELECT * FROM [" + tabName + "$]";
using (OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 12.0;IMEX=1;HDR=Yes'"))
using (OleDbCommand command = new OleDbCommand(sql, connection))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
dataTable = new DataTable(); ;
adapter.Fill(dataTable);
return dataTable;
}
}
catch (Exception ex)
{
}
return dataTable;
}
//method to upload document
public Microsoft.SharePoint.Client.File UploadDocument(DataRow row, string fileName, string filePath )
{
ListItem listItem = null;
string siteurl = "siteurl";
ClientContext ctx = new ClientContext(siteurl);
List libraryObj = ctx.Web.Lists.GetByTitle("libTitle");
Microsoft.SharePoint.Client.File uploadFile = null;
try
{
string DocumentPath = filePath.Trim();
if (!System.IO.File.Exists(DocumentPath))
{
throw new FileNotFoundException();
}
else
{
System.IO.FileStream FileStream = System.IO.File.OpenRead(DocumentPath);
string DocFileName = System.IO.Path.GetFileName(DocumentPath);
ctx.Load(
libraryObj,
library => library.RootFolder.ServerRelativeUrl);
ctx.ExecuteQuery();
using (FileStream fs = System.IO.File.OpenRead(filename))
{
//upload file
Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, libraryObj.RootFolder.ServerRelativeUrl + @"/" + DocFileName, fs, true);
uploadFile = ctx.Web.GetFileByServerRelativeUrl(libraryObj.RootFolder.ServerRelativeUrl + @"/" + DocFileName);
ctx.Load(uploadFile);
ctx.Load(uploadFile.ListItemAllFields);
ctx.ExecuteQuery();
listItem = uploadFile.ListItemAllFields;
ctx.ExecuteQuery();
}
}
if (new FileInfo(DocumentPath).Length == 0)
throw new Exception("File is empty");
//assign value to file's list item from Excel sheet.
listItem["FieldName"] = row["FieldNameinExcel"].ToString().Trim();
//Similarly update other fields
listItem.Update();
ctx.ExecuteQuery();
uploadFile.CheckIn("Checking file in",CheckinType.MajorCheckIn);
ctx.ExecuteQuery();
return uploadFile;
}
catch (Exception ex)
{
throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment