-1

I am getting items from a list using caml query. After that I need to export to excel by csom (c#). I dont want to use pnp .

2
  • That code isn't working,I am looking for better effective code.If you have any leads ,kindly let me know @GaneshSanap Commented Aug 16, 2019 at 12:14
  • I am getting this error on asking {"The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested."} If you have any clue kindly let me know Commented Aug 17, 2019 at 17:45

1 Answer 1

1

"The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested."

This error means you will need to load listItems collection before accessing it, here is a csom code demo to export list item into csv file for your reference:

 static void Main(string[] args)
        {
            string csvFilePath = @"c:\temp\myCSV.csv";
            StringBuilder items = new StringBuilder();
            ClientContext clientContext = new ClientContext("http://sp/sites/dev/");
            List list = clientContext.Web.Lists.GetByTitle("CustomList");
            CamlQuery caml = new CamlQuery();
            ListItemCollection listItems = list.GetItems(caml);
            clientContext.Load(list);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();
            foreach (ListItem listItem in listItems)
            {
                items.Append(listItem.Id + ",");
                items.Append(listItem.FieldValues["Name"]);
                items.AppendLine();
            }
            System.IO.File.WriteAllText(csvFilePath, items.ToString());
        }

enter image description here

enter image description here

1
  • Thanks a lot, but I need to save it in a sharepoint list,without downloading it in local.I need to save it in a sharepoint list. Commented Aug 21, 2019 at 13:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.