Create Event gives null subject and body when upgraded from v4 to v5

Stephen Ketley (Solihull MBC) 0 Reputation points
2024-03-11T09:54:44.93+00:00

I have an application that was using Microsoft.Graph dll v4.54.0 and createing events in Outlook calendars all working fine. I upgraded dll to 5.44.0 (tried all v5 versions). Now the event is created but the subject and body are null

public async Task CreateEventAsync()

{

 // Ensure client isn't null

 _ = _appClient ?? throw new System.NullReferenceException("Graph has not been initialized for user auth");

 var eventNew = new Event

 {

     Subject = Subject,

     IsOrganizer = false,

     Organizer = new Recipient

     {

         EmailAddress = new EmailAddress

         {

             Address = Account

         }

     },

     Attendees = Attendees,

     Start = new DateTimeTimeZone

     {

         DateTime = Start,

         TimeZone = "Europe/London"

     },

     End = new DateTimeTimeZone

     {

         DateTime = End,

         TimeZone = "Europe/London"

     },

     Location = Location,

     Body = new ItemBody

     {

         ContentType = BodyType,

         Content = Body

     },

     IsReminderOn = (ReminderMinutes >= 0),

     ReminderMinutesBeforeStart = (ReminderMinutes >= 0 ? ReminderMinutes : null),

     AllowNewTimeProposals = AllowNewTimeProposals,

     IsOnlineMeeting = IsTeams,

     OnlineMeetingProvider = (IsTeams == true ? OnlineMeetingProviderType.TeamsForBusiness : null)

 };

 try

 {

     //Fxes a bug! "were unable to deserialize"

     var str = JsonConvert.SerializeObject(eventNew);

     Event eventFinal = JsonConvert.DeserializeObject<Event>(str);

    

      EventObject = await _appClient.Users[Account].Calendar.Events.PostAsync(eventFinal);

     **//EventObject.Subject now equals: null**

     **//EventObject.Body now equals:**

     **//<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">**

     **//<meta name="Generator" content="Microsoft Exchange Server">**

     **//<!-- converted from text -->**

     **//<style><!-- .EmailQuote { margin-left: 1pt; padding-left: 4pt; border-left: #800000 2px solid; } --></style></head>**

     **//<body>**

     **//<font size="2"><span style="font-size:11pt;"><div class="PlainText">&nbsp;</div></span></font>**

     **//</body>**

     **//</html>**

     Message = "CREATED: " + EventObject.Id;

 }

 catch (Exception ex)

 {

     EventObject = new Event();

     Message = "Error: " + ex.Message;

 }

}

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,032 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. CarlZhao-MSFT 45,186 Reputation points
    2024-03-13T08:15:41.79+00:00

    Hi @Stephen Ketley (Solihull MBC)

    Try the code snippet below, it works fine for me.

    using Microsoft.Graph;
    using Azure.Identity;
    using Microsoft.Graph.Models;
    using Microsoft.Graph.Models.ODataErrors;
    
    try {
    
        var requestBody = new Event
        {
            Subject = "Let's go for lunch",
            Body = new ItemBody
            {
                ContentType = BodyType.Html,
                Content = "Does mid month work for you?",
            },
            Start = new DateTimeTimeZone
            {
                DateTime = "2024-03-17T12:00:00",
                TimeZone = "Pacific Standard Time",
            },
            End = new DateTimeTimeZone
            {
                DateTime = "2024-03-17T14:00:00",
                TimeZone = "Pacific Standard Time",
            },
            Location = new Location
            {
                DisplayName = "Harry's Bar",
            },
            Attendees = new List<Attendee>
        {
            new Attendee
            {
                EmailAddress = new EmailAddress
                {
                    Address = "******@contoso.com",
                    Name = "Adele Vance",
                },
                Type = AttendeeType.Required,
            },
        },
            TransactionId = "5E163156-7762-4BEB-A1C6-729EA81755A7",
        };
        var result = await graphClient.Users["{user_id}"].Calendar.Events.PostAsync(requestBody);
    }
    catch (ODataError odataError)
    {
        Console.WriteLine(odataError.Error.Code);
        Console.WriteLine(odataError.Error.Message);
    }
    

    User's image

    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.

    0 comments No comments

  2. Stephen Ketley (Solihull MBC) 0 Reputation points
    2024-03-14T10:06:14.32+00:00

    It was the date format, removed fff and added Z

    Start = new DateTimeTimeZone { DateTime = dateTime.AddDays(1).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"), TimeZone = "UTC" }, End = new DateTimeTimeZone { DateTime = dateTime.AddDays(1).AddMinutes(30).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"), TimeZone = "UTC" },

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.