How to use SharePoint 2010 Client Object Model to Managed Client OM

After I have written nice posts on complete details on Client Object Model and ECMAScript introduction planned to write introduction posts on Managed Client OM and Silverlight Client OM. So, in this post, we will discuss on Managed Client Object Model.
So, Managed Client Object Model is simple .NET code written based on CLR. So, you can use C#.net or VB.net to write code and execute whatever you want against SharePoint server.So, you can use any c# project to write code and run it. But, are there any prerequisites to write code? Do we need to follow some rules or process?
First, to write client side code, we need SharePoint libraries which has all the methods and classes to communicate with the SharePoint server. For this reason Microsoft SharePoint 2010 provides us the client side dll's to write Managed code. Below are the details.
DLL's needed: Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll. Find these files in the 14/ISAPI folder. Usually, the location would be at "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI".

How to create a project to start working and communicate to SharePoint 2010 server? Follow the steps below.

  1. Open Visual Studio 2010.
  2. File -> New -> Project -> Visual C# -> Choose Console Application from the list of C# project types. [Make sure you have selected .NET Framework 3.5 on the top of window as shown in below.]

  3. Give some name to the project. I have given some valid name as "SP2010_HelloWorld".
  4. Now next step is getting the SharePoint 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\.
  5. 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.
  6. Finally, our references folder should looks like below.

  7. Now we are ready with all prerequisites and part left is writing code. I will show you simple code on how to write the code for getting web site title and description.
  8. Before start coding, we need add reference to the namespace in page by declaring using keyword as shown below.
using Microsoft.SharePoint.Client; 







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




static void Main(string[] args)   
{
LoadSiteData();
}

private static void LoadSiteData()
{
string webUrl = "http://nb16";
ClientContext context = new ClientContext(webUrl);
Web web = context.Web;
//Loads all web properties.
context.Load(web);
//Execute the query and load the object into the response given in load() method.
context.ExecuteQuery();
Console.WriteLine(web.Title);
Console.ReadLine();
}







NOTE: Remember, you need to change the webUrl to actual SharePoint site url in the above example.



10.  Now run the code and see the output. You will see the title and description of the SharePoint web site.


That's it!!! Very simple and easier way. SharePoint 2010 client side development is very easy now. Lets rock it....