Rajeev Kumar
Rajeev Kumar

Reputation: 371

Unable to update existing calendar event using Graph API

I am trying to update the following fields for an existing event but not updating as expected.

Fields we want to update:

  1. Add and Remove Attachments
  2. Update Subject and Body content.
  3. Add and/or Remove mandatory/optional attendees.

Using the following code to remove an attachment:

await graphClient
    .Users[organizer]
    .Events[organizerEventId]
    .Attachments[attachmentId]
    .Request()
    .DeleteAsync()
    .ConfigureAwait(false);

Using the following code to add an attachment:

var fileAttachment = new Microsoft.Graph.FileAttachment
{
    ODataType = attachment.odataType,
    Name = attachment.name,
    ContentBytes = attachment.contentBytes,
    ContentType = attachment.contentType
};

var response = await graphClient
    .Users[organizer]
    .Events[organizerEventId]
    .Attachments
    .Request()
    .AddAsync(fileAttachment);

Using the following code to update attendees:

var updateEvent = new Microsoft.Graph.Event
{
    Attendees = attendees
};

var resultUpdate = await graphClient
    .Users[organizer]
    .Events[organizerEventId]
    .Request()
    .UpdateAsync(updateEvent);

Using the following code to update Subject and Body content:

var updateEvent = new Microsoft.Graph.Event
{
    HasAttachments = true,
    ResponseRequested = false,
    Subject = subject,
    Body = body
};

var resultUpdate = await graphClient
    .Users[organizer]
    .Events[organizerEventId]
    .Request()
    .UpdateAsync(updateEvent);

I am executing the above codes in sequential order but when I debugged the code I observed that it only executes the first logic to remove attachment and call comes out without executing the remaining code logic written below in the same method.

Upvotes: 0

Views: 646

Answers (1)

Rajeev Kumar
Rajeev Kumar

Reputation: 371

I found the root cause of this issue and fixed it. Actually, the issue was with the async function call. I was trying to call an async function from a non-async function like below:

public void CreateEvent(type parameter1, type parameter2)
{    
     objMyServiceClass.CreateEvent(parameter1, parameter2);
}

I changed the above code to the below code because CreateEvent in MyServiceClass was an async function. Now it started working correctly.

public void CreateEvent(type parameter1, type parameter2)
{    
     Task.Run(async () => await objMyServiceClass.CreateEvent(parameter1, parameter2));
}

I know, it was a very silly mistake but sometimes we developers struggle to find such a silly mistake and ending up with a nightmare. I hope it may help someone. Thanks!

Upvotes: 1

Related Questions