Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Ashikpaul/7d53608601b69a0d8bcd262891b6a75a to your computer and use it in GitHub Desktop.
Save Ashikpaul/7d53608601b69a0d8bcd262891b6a75a to your computer and use it in GitHub Desktop.
Access Sharepoint Lists
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.Office.Interop;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using SP = Microsoft.SharePoint.Client;
namespace ExcelExport
{
class Program
{
static void openfile()
{
string mysheet = @"C:\Users\Ashikp\Desktop\ExceltoSp.xlsx";
var excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbooks books = excelApp.Workbooks;
Excel.Worksheet sheet = books.Open(mysheet).Sheets[1];
Range xlRange = sheet.UsedRange;
int rows = 2;
int rowCount = xlRange.Rows.Count;
int cols = xlRange.Columns.Count;
object[] toExportDetails = new object[95];
int counter = 0;
for (int row = 2; row <= rowCount; row++)
{
for (int col = 1; col <= cols; col++)
{
//Console.WriteLine(" Col {0} => Value: {1}", col, xlRange.Cells[rows, col].Value2);
toExportDetails[counter++] = xlRange.Cells[rows, col].Value2;
}
}
books.Close();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(books);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelApp);
}
static void Main(string[] args)
{
ClientContext clientContext = new
ClientContext("https://rlecoin.sharepoint.com/sites/MEAppraisalsDev/");
string text = System.IO.File.ReadAllText(@"C:\Ashik\ExportToSharepoint\Blah.txt");
var secureString = new SecureString();
foreach (char c in text)
{
secureString.AppendChar(c);
}
clientContext.Credentials = new SharePointOnlineCredentials("[email protected]", secureString);
SP.List EmpoList = clientContext.Web.Lists.GetByTitle("Employees");
SP.List ProjectsoList = clientContext.Web.Lists.GetByTitle("Projects");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = EmpoList.AddItem(itemCreateInfo);
oListItem["Title"] = "My New Item!";
oListItem.Update();
clientContext.ExecuteQuery();
/*foreach (ListItem oListItem in collListItem)
{
Console.WriteLine("ID: {0} \tTitle: {1}\t Employee Name: {2}",
oListItem.Id, oListItem["Title"], oListItem["Employee"]);
}*/
openfile();
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment