Friday, March 14, 2014

Working with LiveSDK

Live SDK enables you to make connect users in to Live account.

As a developer you can access each logged users
  1. Contacts.
  2. Calender.
  3. Sky drive (one drive).
In this post i'm gonna explain how to use Live SDK and demo about how to get Contacts :)


Here we go,
First You need to have Live App
To create live app goto  http://msdn.microsoft.com/en-US/live/  and in there in My Apps tab create your own app .. We need CLIENT_ID for our Mobile Application.

Installing LiveSDK 
  Create New Windows Phone Databound App

  Go to Tools -> Nuget Package Manager -> Package Manager Console In the console then type


 Install – Package LiveSDK
 Or 
You can use Visual Studio Extension Manager and search LiveSDK and install 

Add Login Button to Application
Go to XAML page that yo want to add Login Button and Paste this inside the Header. 

xmlns:live="clr-namespace:Microsoft.Live.Controls;assembly=Microsoft.Live.Controls"


Then add your XAML Button from this code .(Replace CLIENT_ID to your App's CLIENT_ID)
 <live:SignInButton Scopes="wl.basic wl.signin " ClientId="CLIENT_ID" Branding="MicrosoftAccount" SessionChanged="SignInButton_SessionChanged" VerticalContentAlignment="Top" Height="100" Margin="-10,0,10,497" VerticalAlignment="Bottom"/>

Scopes ??
Scopes define what kind of data that you can access through your app. This link provide detailed summery of Scopes in Live Client
http://msdn.microsoft.com/en-us/library/live/hh243646.aspx   

**In My case for just access the contacts I don't need any extended scopes just wl.basic wl.signin is enough.

assume you need to access user calender for that you need to add another field to Scopes as wl.calendars

Then Add This Code part to your C#

private async void SignInButton_SessionChanged(object sender, Microsoft.Live.ControlsLiveConnectSessionChangedEventArgs e)
{
if (e.Status == LiveConnectSessionStatus.Connected)
{
client = new LiveConnectClient(e.Session);
try
{
LiveOperationResult result = await client.GetAsync("me/contacts");
dynamic opresult = result.Result;

List<contact> contactList = new List<contact >();

foreach (var item in opresult.data)
{
contact dummy = new contact();
dummy.Name = item.name;

contactList.Add(dummy);
}

MainLongListSelector.ItemsSource = contactList;
}
catch
{

}
}
}

then Replace your MainPage phone:LongListSelector's Code with this.

<phone:LongListSelector x:Name="MainLongListSelector" Margin="0,150,0,0" ItemsSource="{Binding }" >
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17">
<TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>


We are done.. compile and check :)






















Thanks ...
















Full Code In Media Fire 
http://goo.gl/UrRjY7

 

0 comments: