SharePoint Silverlight Client Object Model

 

Referred URL

http://praveenbattula.blogspot.com/2010/03/sharepoint-2010-silverlight-client.html

In this article we will go through Silverlight Client Object Model. If you want to know the other client object model types go here.ECMAScript and Managed client object models.
To communicate with the SharePoint server in Silverlight context we need to give two client SharePoint DLL references to the silver light project.

DLL's Needed:

Microsoft.SharePoint.Client.Silverlight.dll Microsoft.SharePoint.Client.Silverlight.Runtime.dll.

They can be found at

C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin.

  1. Open Visual Studio 2010.
  2. File -> New -> Project -> Visual C# -> Silverlight -> Select Silverlight Application project template as shown below.

  3. Give some name to the project. In my example, I have given some meaningful name like "SP2010Silverlight_HelloWorld" and create the project.
  4. Now, you see the below screen.

  5. What this meaning is "Do you want to create an ASP.NET web site and host the XAP file generated to the web site". For our example, it's really not needed. But, there is no problem by using that.
  6. Now next step is getting the SharePoint Silverlight Client Dll's reference to our project. So, for this get the SharePoint dll's to the client machine [Where we created project] and paste the DLL's in some safe location. I copied them to C:\SP2010_ClientDLL\.
  7. Now, go to Visual Studio 2010 project right click on project -> select References and browse to location where client dll's copied and select both dll's and hit ok as shown in below figure.

  8. After you added all references the references folder should look like this.
  9. Now we are ready with all prerequisites and part left is with writing code. I will show you simple code on how to write the code for getting web site title and description using Silverlight Client OM.
  10. Before start coding, we need to add reference to the namespace in page by declaring using keyword as shown below.

    using Microsoft.SharePoint.Client; 

  11. This is the code to get data from a SharePoint server, in this example we are retrieving Title and Description of a web site.

It is necessary to add some code in the StartUp event handler to initialize the Microsoft.SharePoint.Client.ApplicationContext with the same initialization parameters and the synchronization context for the current thread (the UI thread). This way, it is also possible to pass initialization parameters from the page to the Silverlight application.

private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
// Initialize the ApplicationContext
ApplicationContext.Init(e.InitParams, System.Threading.
SynchronizationContext.Current);
}

Add Name Space in Project

using Microsoft.SharePoint.Client;

Add the following four private variables:

private ClientContext _context;


Now, it is necessary to add code to execute the following tasks:
1. Connect to the SharePoint server using the Connect method.
2. Connect to the lists available in the SharePoint server using the ConnectLists method.
3. Retrieve data from the ProjectsList2010 list, GetListData method.
4. Load all the available items for the ProjectsList2010 list, LoadItems method.
5. Store a local variable in a list of project instances, one for each item in the ProjectsList2010 list, fill its properties, and then bind the results list to the dataGridProjects DataGrid to display the retrieved data, using the ShowItems method.

private void LayoutRoot_Loaded(Object sender, EventArgs args)
{
_context = new SP.ClientContext
(SP.ApplicationContext.Current.Url);
_context = new SP.ClientContext("http://xpsgaston");
_context.Load(_context.Web);
//_context.Load(_context.Web, website => website.Title);
_context.ExecuteQueryAsync(OnConnectSucceeded, null);
}

private void OnConnectSucceeded(Object sender, SP.ClientRequestSucceededEventArgs args)
{
// This callback isn't called on the UI thread
Dispatcher.BeginInvoke(ConnectLists);
}

 

Second ASync Thread
private void ConnectLists()
{

_context.Load(_context.Web.Lists);

}

You May Also Like

0 comments