How to Get the List of Appointments From the Calendar App in Windows Phone 8

Let us see how to programmatically list all the appointments from the calendar app in Windows Phone 8. Open the Visual Studio 2013 IDE or 2012 IDE and create a new project with a valid name. Here I use “ListAllAppointments Demo” as my project name as shown in the screen below.

ListAllAppointments

Once everything is ready our project looks as in the following screen.

project looks

Now add one Button control to trigger the calendar to get the appointments, change the button name to “Get appointments”.

XAML Code

  1. <phone:PhoneApplicationPage  
  2.     x:Class="ListAllAppointments_Demo.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.     mc:Ignorable="d"  
  10.     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  11.     FontSize="{StaticResource PhoneFontSizeNormal}"  
  12.     Foreground="{StaticResource PhoneForegroundBrush}"  
  13.     SupportedOrientations="Portrait" Orientation="Portrait"  
  14.     shell:SystemTray.IsVisible="True">  
  15.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  16.         <Grid.RowDefinitions>  
  17.             <RowDefinition Height="Auto"/>  
  18.             <RowDefinition Height="*"/>  
  19.         </Grid.RowDefinitions>  
  20.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  21.             <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>  
  22.             <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>  
  23.         </StackPanel>  
  24.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>  
  25.         <Button Content="Get appointments" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="122,66,0,0" Grid.Row="1" Width="286"/>  
  26.     </Grid>  
  27. </phone:PhoneApplicationPage>  
Now go to the code behind page. First we need to add the following reference for getting the user data.
  1. using Microsoft.Phone.Data;  
Next we need to enable the ID_CAP_APPOINTMENTS. Open the WMAppManifest file and enable ID_CAP_APPOINTMENTS as shown as in the following screen.

WMAppManifest

Write the following code to get the appointments.

C# Code
  1. List<Appointment> listofappoinment;  
  2. private void Button_Click(object sender, RoutedEventArgs e)  
  3. {  
  4.    Appointments objAppoinment = new Appointments();  
  5.    objAppoinment.SearchCompleted+=new EventHandler<AppointmentsSearchEventArgs>(objAppoinment_SearchCompleted);  
  6.    DateTime startDate = DateTime.Now;  
  7.    DateTime endDate = startDate.AddDays(20);  
  8.    objAppoinment.SearchAsync(startDate, endDate, null);  
  9. }  
  10. void objAppoinment_SearchCompleted(object sender, AppointmentsSearchEventArgs e)  
  11. {  
  12.    listofappoinment = new List<Appointment>(e.Results);  
  13.    StringBuilder appoinment = new StringBuilder();  
  14.    foreach(Appointment list in listofappoinment)  
  15.    {  
  16.       appoinment.Append("Subject:" + list.Subject +"--"+"Location:" + list.Location);  
  17.       appoinment.Append("|");  
  18.    }  
  19. MessageBox.Show(appoinment.ToString());  
  20. }  
Now run your application. The output looks as in the following screen.

Note:
Before checking the output create an appointment in the emulator. I created two appointments in this demo.

demo


Similar Articles