简体   繁体   中英

[UWP][C#] Create new Calendar on Exchange-type UserDataAccount

I want to create a new Calendar inside my Enterprise Exchange account, but I obtain always the same "Access denied" exception An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.ni.dll but was not handled in user code Additional information: Access denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

This is my code, a lot simplified for this question:

private const string ExchangeActiveSync = "{6833942B-ABDA-4C20-9757-4F9252396BD4}";
UserDataAccountStore userDataAccountStore = await UserDataAccountManager.RequestStoreAsync(UserDataAccountStoreAccessType.AllAccountsReadOnly);

//Read-only access to app user data accounts and system user data accounts.
//AllAccountsReadOnly = 0,
//Read/write access to the current app's user data accounts.
//AppAccountsReadWrite = 1
//So AllAccountsReadWrite is missing :(

IReadOnlyList<UserDataAccount> listUserAccounts = await userDataAccountStore.FindAccountsAsync();

string accountId= (from account in listUserAccounts where account.DeviceAccountTypeId.Equals(ExchangeActiveSync) select account.Id).FirstOrDefault();
//accountId = "29,0,af";
string calendarName = Package.Current.DisplayName;
//calendarName = "Test Calendar"

AppointmentStore appointmentStore = await AppointmentManager.RequestStoreAsync(AppointmentStoreAccessType.AllCalendarsReadWrite);

var calendar = await appointmentStore.CreateAppointmentCalendarAsync(calendarName, accountId);
//Here the exception is thrown

calendar.OtherAppReadAccess = AppointmentCalendarOtherAppReadAccess.Full;
calendar.OtherAppWriteAccess = AppointmentCalendarOtherAppWriteAccess.None;
await calendar.SaveAsync();

I have also added the necessary restricted capabilities to my package.appxmanifest

<rescap:Capability Name="userDataSystem" />
<rescap:Capability Name="userDataAccountsProvider" />
<rescap:Capability Name="appointmentsSystem" />

I think that the main issue is in UserDataAccountStoreAccessType enum. Here we miss the "AllAccountsReadWrite" value (like in the calendars access type). I think that this is an huge limitation. Why I can't create a custom calendar for an account?

I noticed that this limitation is also in System calendar app, so bad :(

There is any solution for this?

Thanks a lot for your support.

best regards,

Flavio

I want to create a new Calendar inside my Enterprise Exchange account, but I obtain always the same "Access denied" exception An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.ni.dll but was not handled in user code Additional information: Access denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

You have used AppointmentStoreAccessType.AllCalendarsReadWrite to get access to appointmentstore. And then you used CreateAppointmentCalendarAsync(calendarName, accountId) method to create calendar. But your account is ReadOnly . So it will throw exception of "Access denied".

If you want to create a new calendar into one existing Exchange account, by testing on my side, it is not allowed currently. If you want to create new appointments into an existing calendar which belongs to a mail account please refer to the following code.

IReadOnlyList<AppointmentCalendar> appointmentCalendars = await appointmentStore.FindAppointmentCalendarsAsync();
foreach (AppointmentCalendar calendar in appointmentCalendars)
{
   if(calendar.DisplayName=="Calendar")
    {
        AppointmentCalendar appointmentcalendar = await appointmentStore.GetAppointmentCalendarAsync(calendar.LocalId);
        await appointmentcalendar.SaveAppointmentAsync(appointment);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM