Hi @Jiya desai
What you need to do is:
- Create a
StringBuilder
object to store the event data. - Write a header line containing the event properties you want to return, such as ID, subject, start time, end time, etc. Separate each property with a comma.
- Iterate through the result object, which is an
IEventCollectionPage
object that contains a list of Event objects. For each Event object, write its properties to the StringBuilder object and separate them with commas. After writing each Event object, start a new line. - Use the
File.WriteAllText()
method to save the contents of the StringBuilder object as a .csv file. Refer to the following code example:
var result = await graphClient.Users["{user_id}"].Calendar.Events.GetAsync();
// Create a string builder to store the event data
var sb = new StringBuilder();
// Write the header row
sb.AppendLine("id,subject,start,end,location,attendees");
// Write the event data
foreach (var e in result.Value)
{
sb.Append(e.Id + ",");
sb.Append(e.Subject + ",");
sb.Append(e.Start.DateTime + ",");
sb.Append(e.End.DateTime + ",");
sb.Append(e.Location.DisplayName + ",");
sb.Append(string.Join(";", e.Attendees.Select( a => a.EmailAddress.Address)));
sb.AppendLine();
}
// Save the string builder content as a .csv file
File.WriteAllText("C:\\events.csv", sb.ToString());
Hope this helps.
If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.