Экспорт SharePoint List как xlsx файл
Пишу библиотеку для взаимодействия с шарпоинтом и на строке worksheet.Cells[rowIndex, columnIndex].Value = item[field.InternalName]; ловлю такой эксепшен: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
public static bool ExportToExcel(this List list, ClientContext context, string outputPath)
{
// Load list items
CamlQuery query = CamlQuery.CreateAllItemsQuery();
ListItemCollection items = list.GetItems(query);
context.Load(items);
context.ExecuteQuery();
// Create new Excel package
using (var excelPackage = new ExcelPackage())
{
var worksheet = excelPackage.Workbook.Worksheets.Add(list.Title);
context.Load(list.Fields);
context.ExecuteQuery();
// Add column headers
int columnIndex = 1;
foreach (var field in list.Fields)
{
if (!field.Hidden)
{
worksheet.Cells[1, columnIndex].Value = field.Title;
columnIndex++;
}
}
// Add data rows
int rowIndex = 2;
foreach (var item in items)
{
columnIndex = 1;
foreach (var field in list.Fields)
{
if (!field.Hidden)
{
worksheet.Cells[rowIndex, columnIndex].Value = item[field.InternalName]; // здесь исключение
columnIndex++;
}
}
rowIndex++;
}
// Save Excel package to file
FileInfo excelFile = new FileInfo(outputPath + list.Title + ".xlsx");
excelPackage.SaveAs(excelFile);
return true;
}
}