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.
They can be found at
C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin.
- Open Visual Studio 2010.
- File -> New -> Project -> Visual C# -> Silverlight -> Select Silverlight Application project template as shown below.
- Give some name to the project. In my example, I have given some meaningful name like "SP2010Silverlight_HelloWorld" and create the project.
- Now, you see the below screen.
- 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.
- 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\.
- 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.
- After you added all references the references folder should look like this.
- 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.
- Before start coding, we need to add reference to the namespace in page by declaring using keyword as shown below.
- 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
Add Name Space in Project
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
_context = new SP.ClientContext("http://xpsgaston");
//_context.Load(_context.Web, website => website.Title);
// This callback isn't called on the UI thread
Second ASync Thread
private void ConnectLists()