Showing posts with label windows 8 app. Show all posts
Showing posts with label windows 8 app. Show all posts

Create a SharePoint Lists reader (Windows Store apps using C# and XAML)

Accessing SharePoint Lists by Using WCF Data Services in Windows 8 app


This walkthrough shows how to create a Windows 8 app that displays SharePoint Announcement, Tasks, Calendar and Documents lists information by using the ListData.svc WCF data service .

You can download the complete Visual Studio project for this tutorial from CodePlex. Download the source code. You can also view the source code on the this page.




Prerequisites


Creating a Windows Store Application

  1. Launch Visual Studio 2012 and select File > New Project.
  2. Create a new Visual C# project using the Grid Application template.
  3. Add a Service Reference. In Solution Explorer, right-click the name of the project and then click Add Service Reference.
  4. In the Address box, type the URL to the target site and append /_vti_bin/ListData.svc.
  5. Change the default name in the Namespace box from ServiceReference1 to something more appropriate, such as SharePointService.
  6. Click OK to create proxy classes, including a data context and entity classes for the lists that you want to access.

Creating the DataSource for the Grid

  1. Right mouse click on the "DataModel" folder and add a new class and call the new class SharePointDataSource.cs
  2. Add the following namespace references. using SharePointLists.SharePointService;
    using System.Data.Services.Client;
    using System.Net;
    using System.Text.RegularExpressions;
    using Windows.UI.Xaml.Media;
    using System.Collections.ObjectModel;
    using Windows.UI.Xaml.Media.Imaging;
    using System.Collections.Specialized;
  3. Add the following variable declarations at the top of the class.
    private static SharePointDataSource _SharePointDataSource = new SharePointDataSource()

    private DataServiceCollection<AnnouncementsItem> announcements = new DataServiceCollection<AnnouncementsItem>();

    private DataServiceCollection<SharedDocumentsItem> sharedDocuments = new DataServiceCollection<SharedDocumentsItem>();

    private DataServiceCollection<CalendarItem> calendar = new DataServiceCollection<CalendarItem>();

    private DataServiceCollection<TasksItem> tasks = new DataServiceCollection<TasksItem>();

    private TeamSiteDataContext context;

    private ObservableCollection<SharePointDataGroup> _allGroups = new ObservableCollection<SharePointDataGroup>();
  4. Replace the constructor with the following:
    public SharePointDataSource()



    {
    context = new TeamSiteDataContext(new Uri("http:///_vti_bin/ListData.svc"));

    NetworkCredential credentials = new NetworkCredential();

    context.UseDefaultCredentials = false;

    context.Credentials = new NetworkCredential("username", "password", "domain");

    announcements = new DataServiceCollection<AnnouncementsItem>();



    announcements.LoadAsync(context.Announcements);
    announcements.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(announcements_LoadCompleted);

    tasks = new DataServiceCollection<TasksItem>();



    tasks.LoadAsync(context.Tasks);
    tasks.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(tasks_LoadCompleted);

    calendar = new DataServiceCollection<CalendarItem>();



    calendar.LoadAsync(context.Calendar);
    calendar.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(calendar_LoadCompleted);

    sharedDocuments = new DataServiceCollection<SharedDocumentsItem>();



    sharedDocuments.LoadAsync(context.SharedDocuments);
    sharedDocuments.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(sharedDocuments_LoadCompleted);



    }
  5. Add the following Load completed procedures for each list:void announcements_LoadCompleted(object sender, LoadCompletedEventArgs e)
    void sharedDocuments_LoadCompleted(object sender, LoadCompletedEventArgs e)
    void tasks_LoadCompleted(object sender, LoadCompletedEventArgs e)
    void calendar_LoadCompleted(object sender, LoadCompletedEventArgs e)
     
  6. Download or view the complete SharePointDataSource.cs here and be sure to replace the "Servername", "username", "password" and "domain" with the server and credentials of your server that's running SharePoint.
  7. Make sure to replace the DataSource on you project. Find all data:SampleDataSource in the xaml files, and replace it with data:SharePointDataSource. And in the xaml.cs files replace all SampleDataSource with SharePointDataSource.
  8. Run your app :)