SharePoint 2010 retrieve files using silverlight client object model

Here is a detailed code snippet for creating a silverlight applictaion that will display all the files\documents from a document library in a webpart using silverlight Client Object model.

To begin with

1. In Visual Studio 2010 create a new Blank Solution. Select .NET Framework 3.5.

2. Next, from the File menu open click on Add -> New Project.

3. In the New Project dialog box, expand the Installed Templates left hand menu to Visual C# -> Silverlight, and choose the Silverlight Application project type in the project type list in the middle section of the screen. Make sure that you have .NET Framework 3.5 selected. Click Ok to continue on Silverlight Application dialog box.

4. Next, add the required references to the Silverlight client object model. To do this browse to “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin” and select Microsoft.SharePoint.ClientSilverlight.dll and Microsoft.SharePoint.Client.Silverlight.Runtime.dll

5. In the solution explorer open the App.xaml.cs and add the following references to the top of the page.

using Microsoft.SharePoint.Client;
using System.Threading;

6. Next in the same file App.xaml.cs, under Applictaion_Startup method add the below to get the refrence to the current sharepoint site and to ensure that we are in sync with the current SharePoint site.

ApplicationContext.Init(e.InitParams, Synchronizationcontext.Current);

7. Next, lets open up our MainPage.xaml and Add two StackPanels under the existing grid.





8. Now, Open MainPage.xaml.cs and add the following using statements to the top of the file:

using Microsoft.SharePoint.Client;

9. Next, add the following code to get all the files from your SharePoint document library in the silverlight webpart.

public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}

ClientContext clientCtx;
Microsoft.SharePoint.Client.List docs;
int FileCounter= 0;

private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
clientCtx = new ClientContext(ApplicationContext.Current.Url);
clientCtx.Load(clientCtx.Web);
clientCtx.Load(clientCtx.Web.Lists);
docs = clientCtx.Web.Lists.GetByTitle(“My Documents”);
clientCtx.Load(docs);
clientCtx.Load(docs.Rootfolder);
clientCtx.Load(docs.Rootfolder.Files);
clientCtx.ExecuteQueryAsync(loadSiteData, null); // We will move to loadSiteData method on success
}

void loadSiteData(Object sender, ClientRequestSucceededEventArgs e)
{
//This method starts on a background thread, but we need to work on UI to display our files Therefore we call Dispatcher.BeginInvoke() to perform the UI updating on the main thread
Dispatcher.BeginInvoke(addFiles);
}

void addFiles()
{
foreach (File doc in docs.Rootfolder.Files)
{
clientCtx.Load(doc);
clientCtx.ExecuteQueryAsync(addFiletoUI, null); // We will move to loadSiteData method on success
}
}

void addFiletoUI(Object sender, ClientRequestSucceededEventArgs e)
{
Dispatcher.BeginInvoke(addFile);
}

void addFile()
{
string filename = docs.Rootfolder.Files[FileCounter].Name;
string fileUrl = docs.Rootfolder.Files[FileCounter].ServerRelativeUrl;
HyperlinkButton fLink= new HyperlinkButton();
fLink.Content = filename;
fLink.NavigateUri = new Uri(fileUrl , UriKind.Relative);
mypanel.Children.Add(fLink);
FileCounter++;
}
}
10. Build and deploy the webpart.
11. Goto SharePoint site and add a silverlight webpart. Point the webpart to “”/_layouts/ClientBin/YourSilverlightApp_Name.xap” and your Done! yeh!

0 comments:

Post a Comment

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com