×
Namespaces

Variants
Actions
Revision as of 04:31, 1 May 2013 by hamishwillee (Talk | contribs)

Birthday Calendar for Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search

This article demonstrates how to integrate Facebook friends birthday events to a calendar using Microsoft Live Connect API.

WP Metro Icon UI.png
WP Metro Icon Web.png
WP Metro Icon Tools.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested with
Devices(s): Nokia Lumia 820,920
Compatibility
Platform(s): Windows Phone
Windows Phone 8
Windows Phone 7.5
Platform Security
Signing Required: Self-Signed
Capabilities: ID_CAP_APPOINTMENTS, ID_CAP_CONTACTS
Article
Keywords: Facebook events
Created: somnathbanik (25 Apr 2014)
Last edited: hamishwillee (01 May 2013)

Contents

Introduction

Windows Phone Calendar API doesn’t provide access to Facebook events/birthday. So the Facebook events/birthday that we see in the device default calendar can’t be replicated into our apps. To overcome this we will use some tips and tricks, and Microsoft Live Connect SDK to get the Facebook birthday events in our app. If you are new to Live Connect SDK, suggest you to visit Getting user information with the Live Connect API for Windows Phone 7 article for better understanding.

Note.pngNote: To fetch the Facabook birthday events using Live Connect SDK, please make sure your Microsoft account is linked with your Facebook account.

Implementation

Create a standard Windows Phone Project

Let’s create a standard Windows Phone Application Project

  • Launch Visual Studio
  • Click on File
  • Click on New Project
  • Select Windows Phone Application (Visual C# Template)
  • Add Name and Location of the project
  • Click OK to create the project.

Adding UI controls on application page

We add a Live SDK SignInButton for login to Microsoft account along with an Image, TextBlock and a Calendar element in MainPage.xaml file.

Note.pngNote: The calendar is created using Telerik RadControls for Windows Phone 8.

  <!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Orientation="Horizontal" Grid.Row="0" Margin="12,0,0,0">
<TextBlock Text="BIRTHDAY CALENDAR" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0,80,0" VerticalAlignment="Top"/>
<live:SignInButton Name="btnSignin" ClientId="Client ID" Branding="Outlook" Scopes="wl.events_create wl.calendars_update wl.basic wl.offline_access wl.signin wl.contacts_birthday wl.calendars" TextType="SignIn" SessionChanged="btnSignin_SessionChanged" HorizontalAlignment="Left" VerticalAlignment="Top" />
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel Orientation="Vertical">
<StackPanel Margin="12,0,0,0" Orientation="Horizontal">
<Image Name="userImage" Height="100" Width="100" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<StackPanel Orientation="Vertical" Margin="12,0,0,0">
<TextBlock Name="userGreetings" Text="" Style="{StaticResource PhoneTextNormalStyle}" ></TextBlock>
<TextBlock Name="userName" Text="" FontWeight="Bold" Style="{StaticResource PhoneTextExtraLargeStyle}"></TextBlock>
</StackPanel>
</StackPanel>
<StackPanel VerticalAlignment="Bottom" Margin="0,0,0,0" >
<telerikInput:RadCalendar SelectedValueChanged="RadCalendar_SelectedValueChanged_1" WeekNumbersVisibility="Visible" Height="450" MonthInfoDisplayMode="Large" VerticalAlignment="Bottom" AppointmentSource="{StaticResource CalendarAppointments}">
</telerikInput:RadCalendar>
<telerikPrimitives:RadDataBoundListBox x:Name="AppointmentsList" Height="129" Margin="-12,8,0,0" EmptyContent="There are no birthday for the selected day.">
<telerikPrimitives:RadDataBoundListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" Margin="0,0,0,12">
<Rectangle Fill="{StaticResource PhoneAccentBrush}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="8" Height="32" Margin="12,0,12,0"/>
<TextBlock Text="{Binding Subject}" FontWeight="Bold" FontFamily="{StaticResource PhoneFontFamilySemiLight}" FontSize="{StaticResource PhoneFontSizeSmall}" Margin="0, -3, 0, -3"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</telerikPrimitives:RadDataBoundListBox.ItemTemplate>
</telerikPrimitives:RadDataBoundListBox>
</StackPanel>
</StackPanel>
</Grid>

Adding code behind

  • Add the following List class to store Facebook birthday events.
   public static class BirthdayData
{
public static List<BirthdayStructure> ListBirthdayData = new List<BirthdayStructure>();
public static string BirthdayCalendarId = null;
}
public class BirthdayStructure
{
public string id { get; set; }
public string name { get; set; }
public string start_time { get; set; }
public string end_time { get; set; }
}
  • Add the following code to create a Live Session and get user information and profile picture.
  // create a session
LiveSession.client = new LiveConnectClient(e.Session);
LiveSession.Session = e.Session;
// get user info
LiveOperationResult operationResultMe = await LiveSession.client.GetAsync("me");
dynamic resultMe = operationResultMe.Result;
//get user profile picture
LiveOperationResult operationResultPicture = await LiveSession.client.GetAsync("me/picture");
dynamic resultPicture = operationResultPicture.Result;
try
{
dynamic meResult = operationResultMe.Result;
if (meResult.first_name != null &&
meResult.last_name != null)
{
userGreetings.Text = "Hello";
userName.Text = meResult.first_name + " " + meResult.last_name + "!";
userImage.Source = new BitmapImage(new Uri(resultPicture.location, UriKind.RelativeOrAbsolute));
}
else
{
userGreetings.Text = "Hello, sign in successful!";
}
}
catch (LiveConnectException exception)
{
userGreetings.Text = "API Error : " +
exception.Message;
}
  • Add the following code to get the user’s calendars id. This code will get all the calendar id added to the use’s account.
                   LiveConnectClient liveClientCal = new LiveConnectClient(LiveSession.session);
LiveOperationResult operationResultCal = await liveClientCal.GetAsync("me/calendars");
dynamic resultCal = operationResultCal.Result;
List<object> dataCal = (List<object>)operationResultCal.Result["data"];
  • Add the following code to check the existence of user’s Birthday calendar and get the next 30 days birthday event details and add it to calendar.
                     if (tempCalName == "Birthday calendar")
{
BirthdayData.BirthdayCalendarId = (string)calInfo["id"];
LiveConnectClient liveClient = new LiveConnectClient(LiveSession.session);
LiveOperationResult operationResult = await liveClient.GetAsync(BirthdayData.BirthdayCalendarId + "/events");
dynamic result = operationResult.Result;
List<object> data = (List<object>)operationResult.Result["data"];
BirthdayData.ListBirthdayData.Clear();
foreach (IDictionary<string, object> bdayInfo in data)
{
string tempTime = "00:00:00";
BirthdayStructure bdayData = new BirthdayStructure();
bdayData.id = (string)bdayInfo["id"];
bdayData.name = (string)bdayInfo["name"];
string tempStartTime = (string)bdayInfo["start_time"];
string[] splitStartTime = tempStartTime.Split(new Char[] { 'T' });
bdayData.start_time = splitStartTime.GetValue(0).ToString()+" "+tempTime;
string tempEndTime = (string)bdayInfo["end_time"];
string[] splitEndTime = tempEndTime.Split(new Char[] { 'T' });
bdayData.end_time = splitEndTime.GetValue(0).ToString()+" "+tempTime;
BirthdayData.ListBirthdayData.Add(bdayData);
}
}
  • Add the following code to display the birthday events of a selected date on the calendar.
private void DisplayAppointmentsForDate(DateTime date)
{
appointmentsSource.FetchData(date, date.AddDays(1));
this.AppointmentsList.ItemsSource = appointmentsSource.GetAppointments((IAppointment appointment) =>
{
DateTime startDate = appointment.StartDate;
DateTime endDate = appointment.EndDate;
DateTime calendarStartDate = date.Date;
DateTime calendarEndDate = date.AddDays(1);
if (calendarStartDate == startDate && calendarEndDate == endDate)
return true;
return false;
});
}
//display all the birthday of a selected date
private void RadCalendar_SelectedValueChanged_1(object sender, ValueChangedEventArgs<object> e)
{
if (e.NewValue == null)
{
this.AppointmentsList.ItemsSource = null;
return;
}
DisplayAppointmentsForDate((e.NewValue as DateTime?).Value);
}

Summary

This example uses the trial version of Telerik RadControls for Windows Phone 8, for production purpose please download the license copy. Most of the code are called from a PhoneClassLibrary which is a part of the project reference. Please download the source code for better understanding of the code flow.

Source Code

364 page views in the last 30 days.
×