Share via

Cannot create an event using application for a user.

SalesOutlook 71 Reputation points
2024-08-08T13:35:23.9766667+00:00

I have a simple graph request to create an event using c# for any user. I have full application permissions on the calendar. I can create emails and SharePoint items so I know the GraphClient is correct. The strange thing is the try catch will not catch the error so I am not sure what the error is. Is there a know issue creating an event using application permissions? The docs say it is supported. I tried multiple Office 365 email accounts.

ApplicationCalendars.ReadWriteApplicationCalendars.ReadWrite var simplerequestBody = new Event

            {

                Subject = "My event",

                Start = new DateTimeTimeZone

                {

                    DateTime = "2024-08-07T21:04:11.549Z",

                    TimeZone = "Pacific Standard Time",

                },

                End = new DateTimeTimeZone

                {

                    DateTime = "2024-08-14T21:04:11.549Z",

                    TimeZone = "Pacific Standard Time",

                },

            };

            try

            {

                var apptresult = await GraphClient.Users["[user email]"].Events.PostAsync(simplerequestBody);

            }

            catch (Exception ex)

            {

                var errorme = ex.Message;

            }
Microsoft 365 and Office | SharePoint | Development
Microsoft Security | Microsoft Graph
0 comments No comments

Answer accepted by question author

CarlZhao-MSFT 46,456 Reputation points
2024-08-09T07:24:50.52+00:00

Hi @SalesOutlook

I just tested this API locally, and it works well. I can use the client credentials flow to create events for other users. Please note that your MS 365 user must have an MS 365 license.

Additionally, I recommend using the OdataError class to catch exceptions.


try
{

var requestBody = new Event
{
	Subject = "Let's go for lunch",
	Start = new DateTimeTimeZone
	{
		DateTime = "2024-08-15T12:00:00",
		TimeZone = "Pacific Standard Time",
	},
	End = new DateTimeTimeZone
	{
		DateTime = "2024-08-15T14:00:00",
		TimeZone = "Pacific Standard Time",
	},
};

var result = await graphClient.Users["{user-id}"].Events.PostAsync(requestBody);

}
catch (ODataError odataError)
{
    Console.WriteLine(odataError.Error.Code);
    Console.WriteLine(odataError.Error.Message);
   
}

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.

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. SalesOutlook 71 Reputation points
    2024-08-09T19:55:30.2533333+00:00

    It eventually creates the appointment. We do not get a value for apptresult to know if it actually worked. And we cannot continue to process unless we know it worked. I will accept answer but it sure is strange behaviour.

    Was this answer helpful?

    0 comments No comments

Your answer

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