Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
Windows Phone Zone is brought to you in partnership with:

Rabeb is a fresh graduate computer engineer. She specializes in User Experience Technologies including Windows Phone 7 and Windows 8, in addition to C# and .NET Technologies. She was selected for the Microsoft reality show Career Factor as Windows Phone 7 Finalist .She is passionate about new technologies . She is an avid proponent of social media , enjoy blogging http://rabeb.wordpress.com and tweeting @Rabeb_Othmani Rabeb is a DZone MVB and is not an employee of DZone and has posted 4 posts at DZone. You can read more from them at their website. View Full User Profile

Using SkyDrive in your Windows Phone applications: Part 2

02.03.2012
Email
Views: 2081
  • submit to reddit
It's so easy to get up and running with your first Windows Phone app!  Just pick up the FREE toolkit from Microsoft, register at AppHub, and start checking out some fundamental tutorials.

This is part two on how to use Skydrive in your Windows Phone applications.  Part one was an introduction to how to sign in into Skydrive.

In this part, let’s try to read the Skydrive’s folders.

I will use the ApplicationBarIconButton to read from my Skydrive then put the result in a very simple list in another page.

So first, let’s create a new Windows Phone page , I’ll call it “ content.xaml”.

I’ll change the Application title to “ Skydrive” and the page title to “ Folders”. I will also add a listbox.

      <!--TitlePanel contains the name of the application and page title-->
         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Skydrive" Style="{StaticResource PhoneTextNormalStyle}"/>
             <TextBlock x:Name="PageTitle" Text="Folders" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
       </StackPanel>

         <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
             <ListBox Name="list">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                       <StackPanel Orientation="Horizontal">
                            <TextBlock Name="folder" Text="{Binding Name}"/>
                        </StackPanel>
                   </DataTemplate>
               </ListBox.ItemTemplate>

             </ListBox>
        </Grid>


Now we need to fill that listBox.

In the MainPage.xaml, let’s add an ApplicationBarIconButton ( it is already there as a commented code):

<phone:PhoneApplicationPage.ApplicationBar>
       <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Read " Click="ApplicationBarIconButton_Click" />
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>
</phone:PhoneApplicationPage>


When you click on the button , you should be able to get your content from Skydrive if you are signed in and see it on that listbox we put in “content.xaml”

private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
   if (session == null)
   {
       infoTextBlock.Text = "You must sign in first.";
   }
   else
   {
       LiveConnectClient client = new LiveConnectClient(session);
        client.GetCompleted +=
            new EventHandler<LiveOperationCompletedEventArgs>(getFolderProperties_Completed);
        client.GetAsync("/me/skydrive/files");
    }

}

void getFolderProperties_Completed(object sender, LiveOperationCompletedEventArgs e)
{
    if (e.Error == null)
    {
        List<object> data = (List<object>)e.Result["data"];
        foreach (IDictionary<string, object> content in data)
        {
            SkyDriveContent skyContent = new SkyDriveContent();
            skyContent.Name = (string)content["name"];
            ContentList.Add(skyContent);
        }
        this.NavigationService.Navigate(new Uri("/content.xaml", UriKind.Relative));
    }
    else
    {
        infoTextBlock.Text = "Error calling API: " + e.Error.Message;
    }

}


The result is stored in a “ SkydriveContent “ list :

public static List<SkyDriveContent> ContentList = new List<SkyDriveContent>();
public class SkyDriveContent
    {   public string Name { get; set; }
        public string Description { get; set; }
    }


In the Content.xaml.cs , we will tell the listbox to get its items from the ContentList:

public content()
{
      InitializeComponent();
      list.ItemsSource = MainPage.ContentList;
}


That is all you need to be able to read your Skydrive folders in your Windows Phone application.

There is also an interesting and helpful post in Silverlight Show by Samidip Basu about the usage of Skydrive with Windows Phone.


Source: http://rabeb.wordpress.com/2012/02/02/using-skydrive-in-your-windows-phone-applications-part2/

References
Published at DZone with permission of Rabeb Othmani, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

The Windows Phone Microzone, which is supported by Microsoft, is your one-stop-shop for news, tutorials, perspectives, and research on the mobile platform that is making waves in smartphone ecosystem.