Getting data out of the device's
calendar store with .NET MAUI has never been easier than with this plugin.
Plugin.Maui.CalendarStore. Let's go check it out. Basically all the platforms that you
can write apps for have some kind of calendar installed on iOS. You have
the Calendar app on Windows, you have a calendar app on Android,
you obviously have Google Calendar and I think some other calendar apps
as well. So that much is clear. You have calendar data on all those
devices. Now, how cool would it be if you can get that data
and use that inside of your own app? That's exactly what
you can do with this new plugin. So you can just reach to that database
because that's basically what it is. Check out the events, the
calendars. You can have multiple on your device and you can list them and use them
inside of your app. Of course, there are some permissions involved
because it's privacy sensitive information. But with this plugin, it's very easy
to get that data out of the platform and inside of your own app. Maybe
you're building your own calendar app. Maybe you want to see if there is a
free spot in the schedule. Well, I leave the app building to you.
Let's just see how this plugin works and how you can apply this to your own
.NET MAUI app. Here we find ourselves in our natural habitat
in Visual Studio 2022, where I created a new .NET MAUI
project. So this is basically the new template that you get out of the
box. I did a couple of things and this is where I'll implement the
plugin. Just so you can see how to get started with this. I already
installed the plugin, so if you go over to the Solution Explorer and
right click on your project, you can do Manage NuGet Packages. And you want to
look for Plugin.Maui.CalendarStore because
Calendar was already taken. That's a wonderful visual control that
you can use. So definitely go check that out as well. I've
released a preview version for now. If you're watching this, by now
maybe a stable version has come out, but it's a preview version right now.
Go check it out and let me know what you think of it and if there's
any bugs and before we can make it stable, basically. So here we
have that and just install that on your project and
you should be good to go. Now you need to add a couple of
permissions as well. So you can go to the Solution Explorer
again to your Platforms folder. This plugin is supported on Android,
iOS, macOS and Windows, not Tizen for the moment. If you are a
Tizen expert, please contribute. But all the other platforms are
working. So for Android, you go into your AndroidManifest, right click
Open with XML Editor, and here you can see the
permission. You can also do it from the graphical editor but this is most
clear to me usually. So read calendar that's in there because this
plugin only reads from the calendar store by now. You also have another plugin.
The AddToCalendar plugin. Plugin.Maui.AddToCalendar. You can write
events with that. So we should probably combine
the functionalities of these two and make it just one, right? That would be very
much clearer. So now we have this for iOS, the same
thing. And for macOS it's done the same thing
as well. So I'm just going to show you iOS. We have the info plist
you can do open with and you can do the XAML editor again,
well, not the XAML one, the XML one. And you
have this key with NSCalendarsUsageDescription. And here you have to
specify the reason why you want to have access to the calendar,
right? You want to ensure your user that you're not doing
anything funny. This is the reason why I want access to your calendar because we're
going to do amazing things with this app. So with that in place, we have
everything to get started. Now there is a couple of ways to go about this.
I've explained this in another video that you might
want to check out on building your own plugins as well. It should pop up on
your screen right now where I go over the Plugin Maui feature is what I
call it. We have a template and those plugins if other authors can
pick it up and change anything they want. But what I like to
do with it is kind of like follow the same pattern as the .NET
MAUI Essentials APIs are following as well. So you can use
this with both dependency injection. In that case you
want to go to your MauiProgram.cs and you want to do here
builder. Services.Add, I guess this could be a singleton. And then you can say ICalendarStore. There we are. And then you can just
say CalendarStore. Default that will register the
instance of this CalendarStore. And you can use
it with dependency injection just as you're used to in .NET MAUI. You can inject
it in your view models and et cetera, et cetera. I'm just going to
use the static instance right now directly because I want to focus on the APIs
and not so much on how to actually use it through dependency
injection or not. So here I have the main page. Nothing has
changed here and I'm going to transform this button into reading something from the
calendar, right? So let's go back to the code behind I'm not going
to use this count anymore. Let's remove that and remove all the counter
clicked here. And what I can do is access it directly. So I can
also here say calendarstore and then it's going to add the using right
here using Plugin.Maui.CalendarStore. And I can have the same default. I can
also have the static methods on here right now that's kind of like all
works the same. It's just however you want to use it. But it used the
exact same code underneath. So I can do GetCalendar, GetCalendars,
GetEvents, right? So that's all the things that
you can get. Right now. I like to go through this default instance right
here and then do this. So get calendar. You can get this with a calendar ID.
So if you know the ID of the calendar that you
want to get, you can just get the calendar right here. This will
return a calendar object with all the properties that come with it. You
can get the calendars that will get a list of calendar. iOS, let's
just do that and you don't have to specify anything,
right, that will just get all the calendars in the calendar store of this device. So here, I want to await this. So I
need to make this async and void for this event as it's not
going to work. Let's capture this in a variable calendars. Got that? And then let's just iterate
over it, right? So my AI helper is going to help me here.
Calendar in calendars. And I'm just going to do a weight display alert, right? Just something simple so we can see
what is going on here. So let's make this a title calendars. And then here we're going to do, I
don't know, a little string interpolation with calendar. It has a couple of only two things, only two
properties. Basically the name and the ID. So the name is the thing that you're
probably going to recognize. And let's add the ID here as well, just so we can
see what's going on. ID. And we need something for the
button. So let's do okay, so what I'm going to do now
is run this on Windows. Let's just do that. And whenever we click the button now, it's going to
list all the calendars that it can find in this calendar store on Windows. So
I think one thing is good to note, one of the questions
that's probably coming up down in the comments below. Hey, can I use this
with Outlook? No, I don't think the Outlook store is connected in the
Windows Store. So you have the calendar app on Windows. That's what this is
going to work with. If you want to use this with Outlook, if you want to
get that calendar data, you probably have APIs to talk to Microsoft Exchange and
Outlook and that kind of stuff. This talks to the local
calendar store on this machine. So whenever I click the button now,
it's going to get me this calendar. Just a generic name calendar with a
very funny looking ID. b,5,13. I don't know. That's the IDs how it
works on the Windows calendar. So I think for Android it has to be
integers. And on iOS I don't even know the IDs will be something different on each platform,
right? So make note of that. So we got that, and I actually got the
calendar for Windows up right here. So you can see this. And today it's
August 31st. So I'm just kind of like giving
away when I'm recording this. So here we have that, and I can add a
calendar here. So here I have this one calendar on the left,
but I can add a new one. And this automatically suggests me,
like, hey, do you want to have the holiday calendars? Well, why not? Let's scroll
down to the Netherlands right here, and it adds the Dutch holiday calendars right here. And
whenever I go back to my app and I do click Me, you can see it immediately
updates. So I have this calendar, and now I have Netherlands, right? So
I have this one. You can add other calendars in here and it
will update automatically. So if I remove this again, I think I could just
delete it here, continue, and if I do it again, I just
have this calendar back, right? So it updates automatically.
I'll show you for Android in a little bit. Let's just focus on
the mechanics here now, first, because this works the same across all the
platforms. That's the power of this plugin and .NET MAUI. Okay, so we got this. Now
let's focus for events because we have this calendar
and you can have a calendar, but you can also get the events right, which is
maybe even more important. So let's basically just copy a bunch
of this code. I like copy and pasting and then make this events. Let's do
that. And then we want to get the events. Now, if you
look at the API here, you have a couple of options,
and it kind of follows the same pattern as the calendars as
well. So you can get all the events for a certain calendar ID, right? So
if you have that calendar ID, you can get all the events for that. And we
have a date time offset for start date and end date, right? So you
can set this range for like, hey, I want to start at this
date and then get everything, all the events, like in the future, or I want
to set this end date and get all the events in the past, right? And
you can mix and match with these parameters. You can see they're all
optional. So just doing this should get you all the events if the platform allows
that. I think it does. So it should do that. But just for the demo,
let's just do something like, I don't know, start date time offset. If you don't know what date
time offset is, let me know down in the comments below and I'll maybe make
a video about it as well. Add days and then we do minus one, right? So we can just have now minus
one day. So we're going to look to yesterday
and then for the end date, let's just set date time offset now add days and let's do it for tomorrow, right? So we
have these three days where we can show the events for and then here
we're going to do foreach ev because I have the e
already, here is the event arguments and if you do event
then it's going to be a keyword. So it's not going to understand that.
So let's just do foreach(var ev in events). And here I want to do events and say
ev. And we don't have a name but we do have
ev.Title. So that's kind of like the name of an
event. And we have all kinds of other things, right?
We have location, we have the start date, we have, I don't know, end date. Is it
an all day event? What attendees are there? So we have a
couple of these things. But here let's do EV title and let's
also get the EV ID. They still have an ID and let's just
add something like hey, this start or something. So EV start date so we can see what's going on. Now
obviously we also have other things. You can also get a specific
event, right? So if I do calendarstore default, you can also do get event, right? So you can get that single
event by an ID as well. So it kind of like follows the same pattern
as the calendar iOS here. Now if I start running this and I go back to
my calendar event, because I think this is tied to my
Microsoft account, it actually says it right here. So
what I said earlier, like getting it from Outlook, it gets it from your
Microsoft account, you should probably be logged in. But I'm not using that calendar
for my personal stuff, right? So there's nothing in here. So
if I do this then probably nothing's going to happen. It's going to show me
that calendar and nothing else. So let's add that event for today, right?
Because I had this at day minus one and plus one and what would
be a good event? Maybe there's a good event in subscribing to this
channel. So maybe you want to do subscribe to Gerald. That's an important one, it shouldn't
take all day. So let's not do that. And we want to set the start date to,
I don't know, 12, maybe during lunch. That's a
good time to watch this video and subscribe
to this channel location, wherever ur should be good. Let's just save that.
And whenever it's saved now it should update in real time. I should go here,
click me, get the calendar. And now you can see get this events as well
with a same kind of ID right here. And you can see that start
date, right? You can see it's a daytime offset so you can see the
actual time zone information as well. 31 August 2023 subscribe to Gerald that's exactly what you should do
right now. Although the date is probably different, but you should still do it.
So now you can get these events as well. And if I remove this, actually,
let me put in a breakpoint here. So for each it's going to get here and
we can see all the other information in here, right? So you can
do that. We hit the breakpoint and then if we hover over ev, you can see all
day is false, attendees is 0. The ID is
this, I didn't enter any description. The duration is 30
minutes. That checks out, right? We entered it for 30 minutes. End date is then this
start date is this location, wherever you are. And
the title is subscribe to Gerald. So all this information about
the events that you can now use inside of your app. Now I promised you
to also show you on Android. So let's get my Android phone
up here. I have here mirrored my physical device, but
it doesn't really matter. So that's lying around right here, you can see it. I'm
not lying to you. See it's mirror actually. And whenever I click it, you
can see it does the same thing. Actually. It asks for the
permission here. So Windows does not need any permissions. Maybe it will come in the
future because it's still sensitive information, but for Android and iOS
and macOS, you should definitely do that. But if you declare the permissions
inside of your apps manifest, it's good, right? The plugin will
request the permissions whenever they are needed. So you can see the permission
coming up here. I can just click Allow and it will still get the calendars.
Now here I have a couple more. This is tied to your Google account
probably. I don't know where it all comes from, but that is the beauty, the
magic, it all comes from the platform that you're running on.
So I have this, my calendar. I have this one which is tied to my
Gmail account. So that's my email for that. Then we have this
one birthdays. And probably this is the
holidays in the Netherlands, right? I have that as well for some
reason. Well, I didn't see this one before.
There is an event coming up on the 10th of September, which
is my birthday. So Google planned an event apparently with Happy Birthday in
there. So now you know my birthday as well. Put that in your calendars
and make me sure to send me a message by then. So there we have
that. So you automatically seen the events as well. Now if I do this
again, it's going to do all the things again, but it doesn't show
me the permissions, right? Because now this app has permissions and it kind
of works the same for iOS and macOS as well. Once you have that
permission, then it's good and you can just access that information
after that. So it also works on Android. And now you know how to
get the data from your calendar on the user's device with.
.NET MAUI. And just like that, you found out what my birthday is. So now you
know when to congratulate me on my birthday. I'm looking forward to
all those messages on any channel you can find me on. Basically, I hope you
enjoyed this one. And from here you can access that data and you can do
all kinds of crazy stuff. Maybe you want to show it in a calendar control.
I have a beautiful video from the DevExpress controls, for instance,
that should show up on your screen right here, where you can show now these
events in your own kind of calendar application. Or maybe you want to
build your own plugin. That's possible as well. Check out this video for that,
and I'll be seeing you in one of those, or maybe both. Until next time!